Home>Article>Backend Development> What to do if php intercepts garbled Chinese characters

What to do if php intercepts garbled Chinese characters

藏色散人
藏色散人 Original
2020-08-06 11:07:06 1790browse

In PHP, you can use the PHP built-in function "mb_substr()" to solve the problem of intercepting garbled Chinese characters. The syntax of this function is "mb_substr($str, $start, $length, $encoding)". The parameter "$str" represents the Chinese string that needs to be truncated.

What to do if php intercepts garbled Chinese characters

Recommendation: "PHP Video Tutorial"

Solution to the garbled Chinese character string intercepted by PHP

When there is a need to intercept a string, the first thing that comes to mind is substr(), but there will be problems with garbled characters. So if you want to intercept a Chinese string, you can use PHP's built-in function Chinese interception: mb_substr (), this is

mb_substr( $str, $start, $length, $encoding )

$str: the Chinese string that needs to be intercepted;

$start: intercept the start parameter, starting at 0;

$length: The number of words to be intercepted;

$encoding: web page encoding, such as UTF-8, GB2312, GBK;

In addition, a function to obtain the length of the Chinese string is attached to obtain the Chinese length: mb_strlen()

mb_strlen( $str, $encoding ) $str : 中文字符串 ; $encoding : 网页编码,如UTF-8,GB2312,GBK;

The above is the detailed content of What to do if php intercepts garbled Chinese characters. 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