Randomly intercepting Chinese characters in PHP

Source: Internet
Author: User
: This article describes the random Chinese characters intercepted by PHP. For more information about PHP tutorials, see. Today, we will share with you how to use PHP's built-in methods to intercept Chinese characters. Note that the method for intercepting Chinese characters is different from that for intercepting Chinese characters, because a Chinese character may have different numbers of characters due to different encoding, which may be 2 or 3 characters! The following describes the specific methods:

Function getH ($ str ){

// Use the preg_split function to Split Chinese character strings into arrays. The first parameter is regular expression matching. u must be added because it is UTF-8 encoded.

// Substr or mb_substr cannot be used here, because these methods are valid for characters and contain 2 or 3 characters.

$ Str = preg_split ('// U', $ str,-1, PREG_SPLIT_NO_EMPTY );

// Use the shuffle function to disrupt the Chinese character array

// The str_shuffle function cannot be used because the character is broken.

Shuffle ($ str );

// Extract the first four elements from the array and obtain an array of Chinese characters.

$ Str = array_slice ($ str, 0, 4 );

// Implode: concatenates an array into a string.
$ Str = implode ('', $ str );
Return $ str;
}
$ Str = 'romantic cherry blossom Wang Jinsheng ';
GetH ($ str );

?>

Although the tutorial is very simple today, the function is still very practical. if it is useful, I would like to appreciate it!


The above describes the random Chinese characters intercepted by PHP, including some content. I hope my friends who are interested in PHP tutorials can help me.

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.