Posted by & filed under Website Development.

I used to try to get substrings of Chinese text and would get partial characters than came out as strange diamonds or squares…. until I found the mb_strcut() function!

When using PHP, avoid using the substr() function, as it will often cut off Chinese and Japanese characters. For example:

$text = '拿小锤锤的那个人好可爱的说,哈哈哈,生活太有爱了,加油加油了';

echo  substr($text, 0, 50); // Outputs: 拿小锤锤的那个人好可爱的说,哈哈��

Rather you should use the mb_strcut() function.

For example:

$text = '拿小锤锤的那个人好可爱的说,哈哈哈,生活太有爱了,加油加油了';

echo mb_strcut($text, 0, 50,'UTF-8'); // Outputs: 拿小锤锤的那个人好可爱的说,哈哈

echo  mb_strcut($text, 15, 30,'UTF-8'); // Outputs: 那个人好可爱的说,哈
  • Allen Tang

    mb_substr will be better.