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
Example #2
As of PHP >= 5.3 you can also declare the encoding directive and use the substr function
Example #3