Tuesday, 15 January 2013

php - substr doesn't work fine with utf8 -



php - substr doesn't work fine with utf8 -

i using substr method access first 20 characters of string. works fine in normal situation, while working on rtl languages (utf8) gives me wrong results (about 10 characters shown). have searched web found nth useful solve issue. line of code:

substr($article['cbody'],0,20);

thanks in advance.

if you’re working strings encoded utf-8 may lose characters when seek part of them using php substr function. happens because in utf-8 characters not restricted 1 byte, have variable length match unicode characters, between 1 , 4 bytes.

you can utilize mb_substr(), works same way substr difference can add together new parameter specify encoding type, whether utf-8 or different encoding.

try this:

$str = mb_substr($article['cbody'], 0, 20, 'utf-8'); echo utf8_decode($str);

hope helps.

php mysql utf-8 right-to-left substr

No comments:

Post a Comment