2

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.

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

@samuel if you find length after utf8_decode then it will give right result :)
1

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; 

1 Comment

Only you cannot use strlen() because that gives you the total number of bytes, not characters. Use mb_strlen() instead.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.