Reputation: 1004
I would like to crop a text string to three characters. Doing so, using $name_short = substr($name, 0, 3);
works great. But as so as the text contains German Umlaute like ä,ö or ü within the first three characters, Würzburg for example is shortened to W&u. Using $name_short = substr(html_entity_decode($name), 0, 3);
works nicely and keeps the Umlaute, but the result is not Wür (for Würzburg), but Wü. I have no idea how to if/else my code correctly to check if there are Umlaute or other special characters from other languages within the first three characters.
So I could crop the string to 4 characters if special characters are found, otherwise to 3.
This made it work:
$name = html_entity_decode($name);
$name_short = mb_substr($name, 0, 3);
Thanks for your help!
Upvotes: 2
Views: 4203
Reputation: 2277
Try mb_substr() instead of substr() like below:
$name = "Würzburg"; $name_short = mb_substr($name, 0, 3);
Upvotes: 7
Reputation: 1398
The problem comes from the fact those special characters (such as ä, ö, ü, œ and others) use more than one byte. So the result you get by using substr()
is technically correct as it returns a 3-byte long string.
Using the multibyte version of the function should fix that problem:
$name_short = mb_substr($name, 0, 3);
Also, as @arkascha mentioned, using html_entity_decode()
will not help you in your situation as it doesn't affect special characters.
mb_substr
doc: http://php.net/manual/en/function.mb-substr.php
Upvotes: 1
Reputation: 4750
This may be an issue of encoding...
You can try mb_substr($name,0,3,'UTF-8');
Upvotes: 2