today we will share with you how to use PHP's own method to intercept Chinese characters . It is important to note that the method of intercepting Chinese characters and intercepting them is different, because a Chinese character can be encoded differently, and the number of characters will be 2 or 3 characters. Let me tell you the exact way:
function Geth ($STR) {
//Use the Preg_split function to split the character string into an array, the first parameter is a regular match and you must add u because it is UTF8 encoded
It is not possible to use substr or MB_SUBSTR, as these methods are valid for characters, and Chinese characters are 2 or 3 characters.
$str = Preg_split ('//u ', $str,-1, preg_split_no_empty);
//Use the shuffle function to disrupt the array of Chinese characters
You cannot use the Str_shuffle function, because that is an scrambled character.
Shuffle ($STR);
//intercept the first 4 elements from the array, get an array of Chinese characters
$str = Array_slice ($str, 0,4);
///implode: putting arrays together into strings
$str = Implode (' ', $str);
return $str;
}
$str = ' Play the romantic cherry blossom Wang Jinsheng ';
Geth ($STR);
?>
Although the tutorial is very simple today, but the function is very practical, feel useful to play a little bit!
The above introduction of PHP random interception of Chinese characters, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.