Home  >  Article  >  Backend Development  >  PHP intercepts string without garbled characters

PHP intercepts string without garbled characters

王林
王林Original
2019-09-26 17:39:451691browse

PHP intercepts string without garbled characters

GBK encoding interception example

$str = '我是谁';  //gbk编码的字符串
echo mb_substr($str, 0, 1, 'gbk'); //输出 我

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

utf-8 encoding interception example

$str = '我abc是谁';  //utf-8编码的字符串
echo mb_substr($str, 0, 2, 'utf-8'); //输出 我a

There is no problem mixing Chinese and English.

Note:

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

2. 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.

Recommended tutorial: PHP video tutorial

The above is the detailed content of PHP intercepts string without garbled 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