Reputation: 3403
I used string[0]
to shorten and show first character of user names/ lastnames. And it worked great until the user's name is starting with a special character like ö, ä, ü.
for example:
$name = 'über';
echo $name[0];
// echoes nothing
Meanwhile I am using mb_substr($string, 0, 1)
instead. But i really miss $name[0]
:). It was much faster and shorter. I still wonder what the reason is and if there is a way to use this syntax with special chars.
Upvotes: 3
Views: 1328
Reputation: 167172
The character ü
is not a single byte character. It is a multi-byte (two byte) character.
You are trying to access the first byte of the character string, so it doesn't work. Just for this very reason, mb_substr
has been designed.
Performs a multi-byte safe
substr()
operation based on number of characters. Position is counted from the beginning of str. First character's position is 0. Second character position is 1, and so on.
If you are just trying to do the $name[0]
, it get's the first character of the multi-byte character, which is just an empty string or a control character to display the ..
above the u
. (take it just for example, like ..
+ u
= ü
).
Upvotes: 8