Substr not working fine with utf8

I use the substr method to access the first 20 characters of a string. It works fine in a normal situation, but when working in rtl (utf8) languages โ€‹โ€‹it gives me incorrect results (about 10 characters shown). I searched on the internet but found nth useful to solve this problem. This is my line of code:

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

Thanks in advance.

+4
source share
2 answers

If you work with strings encoded as UTF-8, you may lose characters when you try to get some of them using the PHP substr function. This is because in UTF-8 characters are not limited to one byte, they have a variable length to match Unicode characters, between 1 and 4 bytes.

You can use mb_substr () . It works almost the same as substr, but the difference is that you can add a new parameter to indicate the type of encoding, whether UTF-8 or another encoding.

Try the following:

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

Hope this helps.

+9
source

Use this instead, here is some extra text to make the body long enough. This will handle multibyte characters. http://php.net/manual/en/function.mb-substr.php

0
source

All Articles