Home >Backend Development >PHP Tutorial >How to intercept Chinese strings without garbled characters in php_PHP Tutorial

How to intercept Chinese strings without garbled characters in php_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:18:48744browse

GBK encoding interception example

Copy code The code is as follows:

$str = 'Who am I'; //gbk encoded string
echo mb_substr($str, 0, 1, 'gbk'); //Output me

The mb_substr method has one more parameter than substr, which is used to specify the string encoding.

utf-8 encoding interception example

[code]
$str = 'Who am I abc'; //utf-8 encoded string
echo mb_substr($str, 0, 2, 'utf-8'); // Output my a
[/code

There is no problem mixing Chinese and English.

Friendly reminder

When using it, pay attention to the encoding of the php file and the encoding when displaying the web page.

To use this mb_substr method, you need to know the encoding of the string in advance. If you don’t know the encoding, you need to judge. The mbstring library also provides mb_check_encoding to check the string encoding, but it is not perfect yet.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/621650.htmlTechArticleGBK encoding interception example copy code The code is as follows: $str = 'Who am I'; //gbk encoded characters String echo mb_substr($str, 0, 1, 'gbk'); //Output my mb_substr method has one more parameter than substr,...
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