PHP: Is it possible to correctly SUBSTR a UTF-8 string

encodingPHPsubstringutf-8

I have (in an SQLite database) the following string:

Лампа в вытяжке на кухне меняется, начиная с вытаскивания белого штырька справа.

The string is correctly shown by PHP using print. I would like to obtain just the first 50 chars of this string, i.e.

Лампа в вытяжке на кухне меняется, начиная с вытас.

I have tried using both the substr and mb_substr, and get

Лампа в вытяжке на кухне ме�, i.e. only 28 chars.

After reading here and elsewhere about the problems of mbstring, I realise that this is actually a 50 byte string (22 Russian chars = 44 bytes plus 5 spaces plus 1 question symbol).

Is there any nice solution to this? All my strings are UTF-8, so I could of course program a substr-function myself, by checking the first bit of every byte etc. But this should surely have been done before, right?

UPDATE: I believe mb_substr does not work properly because mb_detect_encoding() does not work properly.

Best Answer

See below URL:

Extracting a substring from a UTF-8 string in PHP

http://osc.co.cr/extracting-a-substring-from-a-utf-8-string-in-php/

PHP substring with UTF-8

http://greekgeekz.blogspot.in/2010/11/php-substring-with-utf-8.html

Or try it:

Example #1

$str1 = utf8_encode("Feliz día");

$str2 = substr($str1, 0, 9);

echo utf8_decode($str2); 

// will output Feliz d�

Example #2

$str3 = mb_substr($str1, 0, 9, 'UTF-8');

echo utf8_decode($str3); 

// will output Feliz dí

As of PHP >= 5.3 you can also declare the encoding directive and use the substr function

Example #3

declare(encoding='UTF-8');

$str4 = "Feliz día";

$str5 = substr($str4, 0, 9);echo $str5;


// will output Feliz dí