Home > Backend Development > PHP Tutorial > Problem analysis of PHP mb_strwidth in actual use_PHP tutorial

Problem analysis of PHP mb_strwidth in actual use_PHP tutorial

WBOY
Release: 2016-07-15 13:30:39
Original
1171 people have browsed it

The news part of a recent PHP website project needs a function to intercept the string according to the width of the title and add "..." at the end. The first thing that comes to mind is to use PHP's mb _String function implementation, mb_strimwidth, mb_strwidth, and later found that if there is a "" symbol in the title, PHP mb_strwidth will consider the symbol to be 1 width. I wonder if this is not a Chinese double quotation mark. Logically speaking, it must be wide Bytes, the length should be 2 widths, and then query "" unicode is u201C and u201D respectively, which are not in the range of Chinese characters. Then query the code table of unicode.org and find that u2000-u206F is the range of universal symbols. This range Although the characters in are all in the form of wide characters, PHP's mb_ function considers them to be 1 width. We have no choice but to rely on ourselves.

The following are the functions implemented:

<ol class="dp-xml">
<li class="alt"><span><span>function truncString($str, $length)  </span></span></li>
<li><span>{  </span></li>
<li class="alt">
<span>$</span><span class="attribute">countLen</span><span>=</span><span class="attribute-value">0</span><span>;  </span>
</li>
<li>
<span>for($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>;$i</span><span class="tag"><</span><span class="tag-name">mb_strlen</span><span>($str);$i++)  </span></li><li class="alt"><span>{  </span></li><li><span>$countLen+=amb_strwidth(mb_substr($str,$i,1));  </span></li><li class="alt"><span>if($countLen</span><span class="tag">></span><span>$length)  </span>
</li>
<li><span>return mb_substr($str,0,$i);  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>return $str;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>function amb_strwidth($str_width)  </span></li>
<li class="alt"><span>{  </span></li>
<li>
<span>$</span><span class="attribute">count</span><span>=</span><span class="attribute-value">0</span><span>;  </span>
</li>
<li class="alt">
<span>for($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>;$i</span><span class="tag"><</span><span class="tag-name">mb_strlen</span><span>($str_width);$i++)  </span>
</li>
<li><span>{  </span></li>
<li class="alt"><span>//if(mb_substr($str_width,$i,1)=="xE2x80x9C"||mb_substr($str_width,$i,1)=='xE2x80x9D')  </span></li>
<li><span>//如果遇到u2000-u206F内的字符则将计数器加2  </span></li>
<li class="alt"><span>if(preg_match("/[x{2000}-x{206F}]/u",mb_substr($str_width,$i,1)))  </span></li>
<li><span>$count+=2;  </span></li>
<li class="alt"><span>else  </span></li>
<li><span>$count+=mb_strwidth(mb_substr($str_width,$i,1));  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>return $count;  </span></li>
<li class="alt"><span>} </span></li>
</ol>
Copy after login

The above are the specific solutions to problems when using PHP mb_strwidth. I hope it will be helpful to everyone. .


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446292.htmlTechArticleThe news part of a recent PHP website project requires a function to intercept the string according to the width of the title and add it at the end ..., the first thing that comes to mind is to use PHP's mb_string function to implement...
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