PHP intercepts string without garbled characters

王林
Release: 2023-04-07 13:16:01
Original
1814 people have browsed it

PHP intercepts string without garbled characters

GBK encoding interception example

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

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
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!