phpsubstr

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


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?


Solution

  • 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);