However, as long as the character string appears Chinese characters, it may cause PHP substr Chinese garbled characters, because of the Chinese UTF-8 encoding, each Chinese character occupies 3 bytes, while GB2312 occupies 2 bytes, English occupies 1
We know that sometimes use substr to intercept UTF-8 Chinese strings, often garbled, why this problem, this article tells you the answer.
look at this piece of code (character encoding is UTF-8):
Copy CodeThe code is as follows:
$str = ' all know
Differences between strlen, mb_strlen, substr (), mb_substr () and mb_strcut in php. This article describes in detail the differences and usage of strlen, mb_strlen, substr (), mb_substr (), and mb_strcut. For more information, see. About the use of
Understand PHP are aware of strlen and Mb_strlen is the string length of the function, but for some beginners, if not read the manual, perhaps not quite clear the difference.
Understand PHP are aware of strlen and Mb_strlen is the string length of
We know that sometimes use substr to intercept UTF-8 Chinese strings, often garbled, why there is such a problem, this article tells you the answer.
Look at this piece of code (character encoding is UTF-8):
Copy Code code as follows:
This article describes in detail the differences and usage of strlen, mb_strlen, substr (), mb_substr (), and mb_strcut. For more information, see.
Use the following functions to split the mb _ * string:Configuration in winInstall php_mbstring.dll
Example code, using the function substr and ICONV_SUBSTR,MB_SUBSTR
Copy the Code code as follows:
Intercept Chinese string script home _www.jb51.net
$STR = ' 123 Script House 456 welcome everyone's presence ';Echo substr ($str, 0,4);Echo
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.