Home > php教程 > php手册 > body text

PHP函数mb_strcut的使用技巧分享

WBOY
Release: 2016-06-13 11:07:26
Original
1318 people have browsed it

我们在进行PHP函数mb_strcut,他们两个的用法与substr()相似,只是在PHP函数mb_strcut最后要加入多一个参数,以设定字符串的编码,但是一般的服务器都没打开php_mbstring.dll,需要在php.ini在把php_mbstring.dll打开,如果我们不确定有没有开启这功能,那么我们最好先判断一下,if(function_exist(mb_string)).

举个例子:

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

输出:这样一来我的字

PHP函数mb_strcut的示例

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span class="tag-name">php</span><span> </span></span></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

输出:这样一

从上面的例子可以看出,mb_substr是按字来切分字符,而PHP函数mb_strcut是按字节来切分字符,但是都不会产生半个字符的现象,但是有时候在输出的时候也会出现截取的东西不正确,有的是对的有的不对,而且中文还出现乱码,这些现象产生的主要原因是你的这个函数的编方式和你显示他的网页的编码方式不一致而导致的。
这里还介绍一个PHP截取中文字符串的自定义函数:

PHP实现中文字串截取无乱码的方法

<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>$len;$i++){  </span></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

以上就是出现乱码后使用PHP函数mb_strcut解决问题的主要步骤。


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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template