商業源碼熱門下載www.html.org.cn
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.survivalescaperooms.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:我是
新聞熱點
疑難解答