Home > Daily Programming > PHP Knowledge > How to intercept Chinese strings without garbled characters in PHP? (Pictures, text + video)

How to intercept Chinese strings without garbled characters in PHP? (Pictures, text + video)

藏色散人
Release: 2018-10-10 13:49:11
Original
4745 people have browsed it

This article mainly introduces to you the detailed explanation of PHP's method of intercepting Chinese string and without garbled characters.

In the process of learning PHP knowledge, PHP intercepting strings should be a very common basic string operation. I believe everyone is familiar with this knowledge point.

But some novice friends may have encountered garbled characters when intercepting Chinese and English strings. In fact, this is also very easy to solve.

First we need to understand how many bytes Chinese and English occupy.

ASCII code: A Chinese character occupies two bytes of space.

UTF-8 encoding: One Chinese character (including traditional Chinese) is equal to three bytes.

Unicode encoding: One Chinese character (including traditional Chinese) is equal to two bytes.

Below we will use a few simple code examples to introduce in detail the relevant knowledge about PHP intercepting Chinese and English strings without garbled characters.

1. About the substr function to intercept the string

<?php
echo substr("PHP中文网", 0, 5);
Copy after login

substr: Returns the substring of the string.

The first parameter in substr() indicates the string to be intercepted, the second parameter indicates interception starting from position 0, and the third parameter indicates the interception length.

Interception of the first 5 bytes of "PHP Chinese Network", the results are as follows:

How to intercept Chinese strings without garbled characters in PHP? (Pictures, text + video)

As shown in the figure, garbled characters are displayed, which means that when we use the substr function When intercepting Chinese and English strings, garbled characters will appear.

2. About the mb_substr function to intercept strings

<?php
echo mb_substr("PHP中文网", 0, 5);
Copy after login

mb_substr: Get part of the string.

The first five characters of "PHP Chinese Network" are intercepted, and the results are as follows:

How to intercept Chinese strings without garbled characters in PHP? (Pictures, text + video)

The first five characters are intercepted as shown in the figure, and they do not appear. Garbled characters.

Note: mb_substr intercepts the string based on the number of characters.

3. About the mb_strcut function to intercept the string

<?php
echo mb_strcut("PHP中文网", 0, 7);
Copy after login

Intercept the first 7 bytes of "PHP Chinese Network", the results are as follows:

How to intercept Chinese strings without garbled characters in PHP? (Pictures, text + video)

As you can see from the picture, we want to intercept 7 bytes, but only the 6 bytes of "PHP" are intercepted. Since one Chinese character is equal to three bytes, the seventh byte here will not be displayed.

To sum up, if you encounter the need to intercept Chinese strings without garbled characters, you can choose the latter two methods (mb_substr() and mb_strcut()).

This article is an introduction to relevant knowledge about PHPChinese string interception without garbled characters. I hope it will be helpful to friends in need!

If you want to learn more about PHP, you can follow the PHP Chinese website PHP Video Tutorial. Everyone is welcome to learn and refer to it.

The above is the detailed content of How to intercept Chinese strings without garbled characters in PHP? (Pictures, text + video). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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