Troubleshooting the problem of garbled Chinese characters intercepted by PHP substr_PHP Tutorial

WBOY
Release: 2016-07-15 13:31:28
Original
1006 people have browsed it

We are using 1. Using the mb_substr interception of the mbstring extension library will 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.

Give an example of PHP substr intercepting Chinese characters:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><</span><span> ?php  </span></span></li><li><span>echo mb_substr('这样一来我的字符串就不会有乱码^_^', 0, 7, 'utf-8');  </span></li><li class="alt"><span class="tag">?></span><span> </span></span></li>
<li><span>输出:这样一来我的字  </span></li>
<li class="alt">
<span class="tag"><</span><span> ?php  </span></li><li><span>echo mb_strcut('这样一来我的字符串就不会有乱码^_^', 0, 7, 'utf-8');  </span></li><li class="alt"><span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login

Output: like this
As can be seen from the above example, mb_substr splits characters by words, and mb_strcut splits characters by bytes, but will not produce half a character.

How to use PHP substr to intercept Chinese characters without garbled characters

<ol class="dp-xml">
<li class="alt"><span><span>function GBsubstr($string, $start, $length) {  </span></span></li>
<li>
<span>if(strlen($string)</span><span class="tag">></span><span>$length){  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">str</span><span>=</span><span class="attribute-value">null</span><span>;  </span>
</li>
<li>
<span>$</span><span class="attribute">len</span><span>=$start+$length;  </span>
</li>
<li class="alt">
<span>for($</span><span class="attribute">i</span><span>=$start;$i</span><span class="tag"><</span><span>$len;$i++){  </span></li><li><span>if(ord(substr($string,$i,1))</span><span class="tag">></span><span>0xa0){  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">str.</span><span>=</span><span class="attribute-value">substr</span><span>($string,$i,2);  </span>
</li>
<li><span>$i++;  </span></li>
<li class="alt"><span>}else{  </span></li>
<li>
<span>$</span><span class="attribute">str.</span><span>=</span><span class="attribute-value">substr</span><span>($string,$i,1);  </span>
</li>
<li class="alt"><span>}  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>return $str.'...';  </span></li>
<li><span>}else{  </span></li>
<li class="alt"><span>return $string;  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>}  </span></li>
</ol>
Copy after login

The above two code examples are the reasons and solutions for the garbled characters when PHP substr intercepts Chinese characters. introduce.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446215.htmlTechArticleWe are using 1. Using the mb_substr interception of the mbstring extension library will not cause garbled characters. 2. Write the interception function yourself, but the efficiency is not as high as using the mbstring extension library. 3. If only...
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!