Home  >  Article  >  Backend Development  >  What to do if the php string part is garbled

What to do if the php string part is garbled

藏色散人
藏色散人Original
2023-01-20 10:18:083087browse

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.

What to do if the php string part is garbled

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!

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