PHP的mb

來源:互聯網
上載者:User

php的mbstring擴充模組提供了多位元組字元的處理能力,平常最常用的就是用mbstring來切分多位元組的中文字元,這樣可以避免出現半個字元的情況,由於是php的擴充,它的效能也要比一些自訂的多位元組切分函數要好上一些。

mbstring extension提供了幾個功能類似的函數,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');
?>
輸出結果如下:

mb_substr:我是一串比較
mb_strcut:我是



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。