欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺析PHP substr,mb_substr以及mb_strcut的區(qū)別和用法

 更新時(shí)間:2013年06月21日 18:09:45   作者:  
本篇文章是對PHP中的substr,mb_substr以及mb_strcut區(qū)別和用法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
PHP substr()函數(shù)可以 分割文字,但要分割的文字如果包括中文字符往往會(huì)遇到問題,這時(shí)可以用mb_substr()/mb_strcut這個(gè)函數(shù),mb_substr() /mb_strcut的用法與substr()相似,只是在mb_substr()/mb_strcut最后要加入多一個(gè)參數(shù),以設(shè)定字符串的編碼,但是 一般的服務(wù)器都沒打開php_mbstring.dll,需要在php.ini在把php_mbstring.dll打開。
舉個(gè)例子:
復(fù)制代碼 代碼如下:

<?php
echo mb_substr('這樣一來我的字符串就不會(huì)有亂碼^_^', 0, 7, 'utf-8');
?>

輸出:這樣一來我的字
復(fù)制代碼 代碼如下:

<?php
echo mb_strcut('這樣一來我的字符串就不會(huì)有亂碼^_^', 0, 7, 'utf-8');
?>

輸出:這樣一
從上面的例子可以看出,mb_substr是按字來切分字符,而mb_strcut是按字節(jié)來切分字符,但是都不會(huì)產(chǎn)生半個(gè)字符的現(xiàn)象……
mbstring 函數(shù)的說明:
php的mbstring擴(kuò)展模塊提供了多字節(jié)字符的處理能力,平常最常用的就是用mbstring來切分多字節(jié)的中文字符,這樣可以避免出現(xiàn)半個(gè)字符的情況,由于是php的擴(kuò)展,它的性能也要比一些自定義的多字節(jié)切分函數(shù)要好上一些。
mbstring extension提供了幾個(gè)功能類似的函數(shù),mb_substr和mb_strcut,看看手冊上對它們的解釋。
復(fù)制代碼 代碼如下:

mb_substr
mb_substr() returns the portion of str specified by the start and length parameters.
mb_substr() performs multi-byte safe substr() operation based on number of characters. Position is counted from the beginning of str. First character's position is 0. Second character position is 1, and so on.
mb_strcut
mb_strcut() returns the portion of str specified by the start and length parameters.
mb_strcut() performs equivalent operation as mb_substr() with different method. If start position is multi-byte character's second byte or larger, it starts from first byte of multi-byte character.
It subtracts string from str that is shorter than length AND character that is not part of multi-byte string or not being middle of shift sequence.

再舉個(gè)例子,有一段文字, 分別用mb_substr和mb_strcut來做切分:
PLAIN TEXT
CODE:
復(fù)制代碼 代碼如下:

<?php
$str = '我是一串比較長的中文-www.webjx.com';
echo "mb_substr:" . mb_substr($str, 0, 6, 'utf-8');
echo "<br>";
echo "mb_strcut:" . mb_strcut($str, 0, 6, 'utf-8');
?>

輸出結(jié)果如下:
mb_substr:我是一串比較
mb_strcut:我是

相關(guān)文章

最新評論