Reputation: 527
I am having a word like "Stefan Äöüéèêë". I want to truncate this string after 20 characters...but it is giving me like Stefan Äöüéèê� . I am using following code for this.
$string = "Stefan Äöüéèêë";
if(strlen($string) > 20){
$string = substr($string, 0, 20) . '...';
}
How can I get string which is safely truncated.
Upvotes: 2
Views: 362
Reputation: 1894
try this friend , if you decode the string then it will give proper length of string.
$string = "Stefan Äöüéèêë";
$string= utf8_decode($string);
echo strlen($string);
if(strlen($string) > 10){
$string = mb_substr($string, 0, 10);
}
echo $string;
Upvotes: 1
Reputation: 6500
Use the multibyte version of it: http://php.net/manual/de/function.mb-substr.php
Further more here is a list of unicode unsafe methods that is quite handy: http://www.phpwact.org/php/i18n/utf-8
Note: strlen does return the wrong value
Upvotes: 2