Problem with diacritics and mb_substr

I slice the unicode string with diacritics using a function mb_substr, but it works the way I would use a simple function substr. It breaks Unicode characters, half displaying a question diamond.

eg.

echo mb_substr('ááááá', 0, 5); //Displays áá 

What could be wrong?

+3
source share
2 answers

I have the same problem if I don’t specify the encoding as the last parameter mb_substr: it is by default, at least to my server, before ISO-8859-1.


But, if I set the encoding correctly UTF-8, it works fine:

echo mb_substr('ááááá', 0, 5, 'UTF-8');

Returns the desired screen in the browser:

ááááá


. mb_substr (, ):

string mb_substr  ( string $str  , int $start  [, 
    int $length  [, string $encoding  ]] )

encoding . , .

+6

, . php.ini ini_set(), mb_internal_encoding('utf-8'); (utf-8 ) .

+1

Source: https://habr.com/ru/post/1728604/


All Articles