淺析PHP substr,mb_substr以及mb_strcut的區(qū)別和用法
更新時間:2013年06月21日 18:09:45 作者:
本篇文章是對PHP中的substr,mb_substr以及mb_strcut區(qū)別和用法進行了詳細的分析介紹,需要的朋友參考下
PHP substr()函數(shù)可以 分割文字,但要分割的文字如果包括中文字符往往會遇到問題,這時可以用mb_substr()/mb_strcut這個函數(shù),mb_substr() /mb_strcut的用法與substr()相似,只是在mb_substr()/mb_strcut最后要加入多一個參數(shù),以設定字符串的編碼,但是 一般的服務器都沒打開php_mbstring.dll,需要在php.ini在把php_mbstring.dll打開。
舉個例子:
<?php
echo mb_substr('這樣一來我的字符串就不會有亂碼^_^', 0, 7, 'utf-8');
?>
輸出:這樣一來我的字
<?php
echo mb_strcut('這樣一來我的字符串就不會有亂碼^_^', 0, 7, 'utf-8');
?>
輸出:這樣一
從上面的例子可以看出,mb_substr是按字來切分字符,而mb_strcut是按字節(jié)來切分字符,但是都不會產(chǎn)生半個字符的現(xiàn)象……
mbstring 函數(shù)的說明:
php的mbstring擴展模塊提供了多字節(jié)字符的處理能力,平常最常用的就是用mbstring來切分多字節(jié)的中文字符,這樣可以避免出現(xiàn)半個字符的情況,由于是php的擴展,它的性能也要比一些自定義的多字節(jié)切分函數(shù)要好上一些。
mbstring extension提供了幾個功能類似的函數(shù),mb_substr和mb_strcut,看看手冊上對它們的解釋。
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.
再舉個例子,有一段文字, 分別用mb_substr和mb_strcut來做切分:
PLAIN TEXT
CODE:
<?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:我是
舉個例子:
復制代碼 代碼如下:
<?php
echo mb_substr('這樣一來我的字符串就不會有亂碼^_^', 0, 7, 'utf-8');
?>
輸出:這樣一來我的字
復制代碼 代碼如下:
<?php
echo mb_strcut('這樣一來我的字符串就不會有亂碼^_^', 0, 7, 'utf-8');
?>
輸出:這樣一
從上面的例子可以看出,mb_substr是按字來切分字符,而mb_strcut是按字節(jié)來切分字符,但是都不會產(chǎn)生半個字符的現(xiàn)象……
mbstring 函數(shù)的說明:
php的mbstring擴展模塊提供了多字節(jié)字符的處理能力,平常最常用的就是用mbstring來切分多字節(jié)的中文字符,這樣可以避免出現(xiàn)半個字符的情況,由于是php的擴展,它的性能也要比一些自定義的多字節(jié)切分函數(shù)要好上一些。
mbstring extension提供了幾個功能類似的函數(shù),mb_substr和mb_strcut,看看手冊上對它們的解釋。
復制代碼 代碼如下:
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.
再舉個例子,有一段文字, 分別用mb_substr和mb_strcut來做切分:
PLAIN TEXT
CODE:
復制代碼 代碼如下:
<?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:我是
您可能感興趣的文章:
- php中的一個中文字符串截取函數(shù)
- PHP中英混合字符串截取函數(shù)代碼
- 多個PHP中文字符串截取函數(shù)
- php中支持多種編碼的中文字符串截取函數(shù)!
- php字符串截取函數(shù)用法分析
- php自定義中文字符串截取函數(shù)substr_for_gb2312及substr_for_utf8示例
- PHP中文處理 中文字符串截取(mb_substr)和獲取中文字符串字數(shù)
- PHP截取漢字亂碼問題解決方法mb_substr函數(shù)的應用
- php mb_substr()函數(shù)截取中文字符串應用示例
- php截取字符串函數(shù)substr,iconv_substr,mb_substr示例以及優(yōu)劣分析
- php字符串截取函數(shù)mb_substr用法實例分析
相關(guān)文章
PHP提示Deprecated: mysql_connect(): The mysql extension is dep
這篇文章主要介紹了PHP提示Deprecated: mysql_connect(): The mysql extension is deprecated的解決方法,是在進行PHP數(shù)據(jù)庫程序開發(fā)中常會遇到的錯誤,需要的朋友可以參考下2014-08-08PHP調(diào)用JAVA的WebService簡單實例
本篇文章主要是對PHP調(diào)用JAVA的WebService簡單實例進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-03-03php XPath對XML文件查找及修改實現(xiàn)代碼
php XPath對XML文件查找及修改實現(xiàn)代碼,需要的朋友可以參考下。2011-07-07探討如何使用SimpleXML函數(shù)來加載和解析XML文檔
本篇文章是對使用SimpleXML函數(shù)來加載和解析XML文檔進行了詳細的分析介紹,需要的朋友參考下2013-06-06PHP has encountered an Access Violation at 7C94BD02解決方法
PHP has encountered an Access Violation at 7C94BD02解決方法2009-08-08php函數(shù)之strtr和str_replace的用法詳解以及效率分析
PHP中主要用strtr()和str_repalce()這兩個函數(shù)替換字符串和數(shù)組,但你們都知道他們這兩個函數(shù)的區(qū)別和用法嗎?有不少文章在說使用strtr函數(shù)比str_replace快4倍,那為什么很多時候都在用str_replace,到底應該使用哪個函數(shù)呢2022-11-11