Home > Article > Backend Development > Chinese substr in PHP is garbled?
#The substr in PHP is garbled in Chinese?
The substr Chinese characters in PHP are garbled. The reason is that the "substr" function splits Chinese characters according to the length of 3, so it will cause garbled characters. The solution is to use the function in the "mbstring" extension. "mb_substr", this function will calculate a Chinese character as length 1.
Usage examples
<?php header("Content-Type: text/html; charset=utf-8"); $str="php中文网php中文网php中文网php中文网"; echo strlen($str)."<br>"; echo substr($str,0,12)."<br>"; echo mb_strlen($str,"UTF8")."<br>"; echo mb_substr($str,0,12,"UTF8")."<br>"; ?>
Recommended tutorial: "PHP"
The above is the detailed content of Chinese substr in PHP is garbled?. For more information, please follow other related articles on the PHP Chinese website!