Using PHP's substr() with special characters at the end results in question marks

Bert picture Bert · Jun 6, 2011 · Viewed 12.8k times · Source

When I use the substr() function in PHP, I get an question mark (a square with a question mark - depending on the browser) at the end of the string when this last character was a special one, like ë or ö, etc...

$introtext = html_entity_decode($item->description, ENT_QUOTES, "UTF-8");
$introtext = substr($introtext, 0, 200);

How can I escape this?

Answer

lonesomeday picture lonesomeday · Jun 6, 2011

If your string has multibyte encoding (like UTF-8) does, you should use mb_substr to avoid problems like this:

$introtext=mb_substr($introtext,0,200);