PHP random interception of Chinese characters

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.