Home  >  Article  >  Backend Development  >  Chinese substr in PHP is garbled?

Chinese substr in PHP is garbled?

Guanhui
GuanhuiOriginal
2020-07-22 16:06:272860browse

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

";

echo substr($str,0,12)."
"; echo mb_strlen($str,"UTF8")."
"; echo mb_substr($str,0,12,"UTF8")."
"; ?>

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn