Home > Backend Development > PHP Tutorial > PHP Chinese string interception method analysis, _PHP tutorial

PHP Chinese string interception method analysis, _PHP tutorial

WBOY
Release: 2016-07-13 10:17:58
Original
889 people have browsed it

php Chinese string interception method analysis,

Using the PHP function substr to intercept Chinese characters may cause garbled characters, mainly because substr may forcibly convert a Chinese character " Saw" in half.
Solution:

1. Use the mb_substr interception of the mbstring extension library to avoid garbled characters.

2. Write the interception function yourself, but the efficiency is not as high as using the mbstring extension library.

3. If it is just to output the intercepted string, it can be implemented in the following way: substr($str, 0, 30).chr(0).

The substr() function can split text, but if the text to be split includes Chinese characters, you will often encounter problems. In this case, you can use the mb_substr()/mb_strcut function. The usage of mb_substr()/mb_strcut is the same as that of substr () is similar, except that one more parameter needs to be added at the end of mb_substr()/mb_strcut to set the encoding of the string. However, most servers do not open php_mbstring.dll. You need to open php_mbstring.dll in php.ini.

For example:

<?<span>php
</span><span>echo</span> mb_substr('这样一来我的字符串就不会有乱码^_^', 0, 7, 'utf-8'<span>);
</span>?>
Copy after login

Output: This way my words

<?<span>php
</span><span>echo</span> mb_strcut('这样一来我的字符串就不会有乱码^_^', 0, 7, 'utf-8'<span>);
</span>?>
Copy after login

Output: like this

mb_substr splits characters by words, while mb_strcut splits characters by bytes, but neither will produce half a character.

How to intercept Chinese text strings without garbled characters using PHP

<?<span>php
</span><span>//</span><span>此函数完成带汉字的字符串取串</span>
<span>function</span> substr_CN(<span>$str</span>,<span>$mylen</span><span>){ 
</span><span>$len</span>=<span>strlen</span>(<span>$str</span><span>);
</span><span>$content</span>=''<span>;
</span><span>$count</span>=0<span>;
</span><span>for</span>(<span>$i</span>=0;<span>$i</span><<span>$len</span>;<span>$i</span>++<span>){
</span><span>if</span>(<span>ord</span>(<span>substr</span>(<span>$str</span>,<span>$i</span>,1))>127<span>){
</span><span>$content</span>.=<span>substr</span>(<span>$str</span>,<span>$i</span>,2<span>);
</span><span>$i</span>++<span>; // www.jbxue.com
}</span><span>else</span><span>{
</span><span>$content</span>.=<span>substr</span>(<span>$str</span>,<span>$i</span>,1<span>);
}
</span><span>if</span>(++<span>$count</span>==<span>$mylen</span><span>){
</span><span>break</span><span>;
}
}
</span><span>echo</span> <span>$content</span><span>;
}

 

</span><span>$str</span>="34中华人民共和国56"<span>;
substr_CN(</span><span>$str</span>,3);<span>//</span><span>输出34中</span>
?>
Copy after login

Reference article:

Detailed explanation of substr string interception function examples in php

Usage of substr string interception function in php

Support Chinese php string interception function

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/886486.htmlTechArticlephp Chinese string interception method analysis, using the PHP function substr to intercept Chinese characters may cause garbled characters, mainly because substr may Forcibly sawing a Chinese character in half. Solution...
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