TiMESPLiNTER
TiMESPLiNTER

Reputation: 5899

Access specific characters in UTF-8 string

A questions answer here leaded me to the following "problem" or challenge:

Is it somehow possible to get a character from a specfic position if the string is UTF-8 encoded and contains special chars.

So for non-special char containing strings this works:

$str = 'abcd';
echo $str{1}; // will print "b"

But for a string like this:

$str = 'abc★';
echo $str{1}; // will return "b"
echo $str{3}; // leads to a question mark

Of course the PHP file is encoded in UTF-8 and <meta charset="utf-8"> is in the head of the HTML.

So is there any solution to get this method of catching a char in the string working?

Upvotes: 0

Views: 340

Answers (1)

georg
georg

Reputation: 215009

One possible way

$str = 'abc★';

preg_match_all('/./su', $str, $m);
$chars = $m[0];

echo $chars[1]; // b
echo $chars[3]; // ★

/./su means "any character, including newline ("s"), in utf8 mode ("u")".

Or like this

echo mb_substr($str, 3, 1, 'utf8'); // ★

Upvotes: 1

Related Questions