Home>Article>Backend Development> What to do if the php string part is garbled
Solution to the garbled part of the php string: 1. Use "mb_substr(strip_tags($str),0,-1,'UTF-8');" to intercept the string; 2. Use "iconv( "UTF-8","GB2312//IGNORE",$data)" can convert the character set.
The operating environment of this tutorial: Windows 10 system, PHP version 8.1, DELL G3 computer
What to do if the php string part is garbled ?
Solution to garbled characters in PHP strings
If string interception causes garbled strings, use mb_substr to intercept them
Parameters
str Required. Extracts a substring from this string.
start Required. Specifies where in the string to begin.
Positive number - starts at the specified position in the string
Negative number - starts at the specified position from the end of the string
0 - at the first character in the string Start at
length Optional. Specifies the length of the string to be returned. The default is until the end of the string.
Positive number - returned from the position of the start parameter
Negative number - returned from the end of the string
encoding Optional. Character Encoding. If omitted, the internal character encoding is used.
$str = '表现出扎实的基�' $str = mb_substr(strip_tags($str),0,-1,'UTF-8'); echo $str; // 输出内容为 表现出扎实的基
Or use iconv to convert the character set
iconv("UTF-8","GB2312//IGNORE",$data)
ignore means to ignore errors during conversion. If there is no ignore parameter, all strings following the character will not be able to is saved.
Convert character set
$str=mb_convert_encoding('转换内容',"utf-8","GBK");
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What to do if the php string part is garbled. For more information, please follow other related articles on the PHP Chinese website!