PHP substr () function can split the text, but to split the text if the inclusion of Chinese characters tend to encounter problems, you can use MB_SUBSTR ()/mb_strcut This function, Mb_substr ()/mb_strcut usage and substr () similar, Just in Mb_substr ()/mb_strcut to add more than one parameter to set the string encoding, but the general server did not open Php_mbstring.dll, need to php.ini in the Php_mbstring.dll to open.
As an example:
Copy CodeThe code is as follows:
Echo mb_substr (' This way my string will not have garbled ^_^ ', 0, 7, ' utf-8 ');
?>
Output: so that my word
Copy CodeThe code is as follows:
Echo mb_strcut (' This way my string will not have garbled ^_^ ', 0, 7, ' utf-8 ');
?>
Output: Such a
As can be seen from the above example, MB_SUBSTR is the word to divide the characters, and mb_strcut is to divide the characters by Byte, but will not produce a half-character phenomenon ...
description of the mbstring function:
PHP's mbstring Extension module provides multi-byte character processing ability, usually the most commonly used is to use the mbstring to split multi-byte Chinese characters, so as to avoid the occurrence of half a character, because it is the extension of PHP, its performance is better than some custom multi-byte segmentation function.
Mbstring extension provides several functions similar to functions, Mb_substr and mb_strcut, to look at the manuals for explanations of them.
Copy CodeThe code is as follows:
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 to shorter than length and character that's not part of the multi-byte string or not being middle of shift sequence.
For example, there is a piece of text that uses MB_SUBSTR and mb_strcut to do the slicing:
PLAIN TEXT
CODE:
Copy CodeThe code is as follows:
$str = ' I am a long string of Chinese-www.webjx.com ';
echo "MB_SUBSTR:". Mb_substr ($str, 0, 6, ' utf-8 ');
echo "
";
echo "Mb_strcut:". Mb_strcut ($str, 0, 6, ' utf-8 ');
?>
The output results are as follows:
MB_SUBSTR: I'm a bunch of comparisons
Mb_strcut: I am
http://www.bkjia.com/PHPjc/327759.html www.bkjia.com true http://www.bkjia.com/PHPjc/327759.html techarticle PHP substr () function can split the text, but to split the text if the inclusion of Chinese characters will often encounter problems, then you can use MB_SUBSTR ()/mb_strcut This function, Mb_substr ()/mb_strcu ...