This article mainly introduces to you the detailed explanation ofPHP's method of intercepting Chinese stringandwithout garbled characters.
In the process of learning PHP knowledge,PHP intercepting stringsshould 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 aboutPHP intercepting Chinese and English strings without garbled characters.
1. About the substr function to intercept the string
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:
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
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:
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
Copy after login
Intercept the first 7 bytes of "PHP Chinese Network", the results are as follows:
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 websitePHP 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!