开发者

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

开发者 https://www.devze.com 2023-03-10 15:43 出处:网络
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 specia

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?


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


In case someone tried the previous answers, and it still didn't work:

Try to add a Unicode name in mb_substr like:

$introtext = mb_substr($introtext, 0, 200, 'utf-8');


Use mb_substr


That is because substr does not work with multibyte characters. substr will probably cut a multibyte character "in half". You should instead use mb_substr. Also make sure that your file is saved in UTF-8.

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


use mb_substr instead of substr that solves problems like that, but before that check mb_string is enabled in your PHP configuration by:

php -i | grep mbstring

It would show you that the mb_string is enabled or not. If not you can install that by: (for PHP 8.0)

sudo apt-get install php8.0-mbstring

Now you can use mb_substr like this:

mb_substr(string $string, int $start, int $length, string $encoding): string

$introtext = mb_substr($introtext, 0, 200, 'UTF-8');
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号