Php extracts Chinese characters, letters, and numbers from strings

Source: Internet
Author: User
I use interfaces to read user nicknames, but many of the names are special characters. for example, it doesn't make sense to save these special characters into the mysql database. So I want to filter it out. only Chinese characters and numbers are extracted from the name characters. How to write this in PHP.

I use interfaces to read user nicknames, but many of the names are special characters. for example, it doesn't make sense to save these special characters into the mysql database. So I want to filter it out. only Chinese characters and numbers are extracted from the name characters. How to write this in PHP.

Reply content:

I use interfaces to read user nicknames, but many of the names are special characters. for example, it doesn't make sense to save these special characters into the mysql database. So I want to filter it out. only Chinese characters and numbers are extracted from the name characters. How to write this in PHP.

It makes no sense that these special characters are not stored in the mysql database.
---- Meaningful, ICON. use varbinary for the field type.

In fact, emoji is not stored in mysql.
If this type of expression is not processed, an error will be reported if it is directly stored in versions earlier than MySQL.
You can modify the database character set to utf8mb4.
Github has the range for extracting emoji unicode. you can match the range when filtering.

First of all, if these things do not make any sense to you, you can just leave them empty.

It is useless for you to extract the part.

The complete access is supported by mysql, and the character set can be converted. utf8mb4 is the superset of utf8, which is backward compatible. modifying this is the perfect solution.

The second is code-level transcoding, encoding, and saving. It can also be pulled out for reverse encoding and then displayed.

The last step is this method. In fact, you just cannot store it in emoji. If you filter out emoji, it will be OK.

public static function emoji($text) {        $clean_text = "";        // Match Emoticons        $regexEmoticons = '/[\x{1F600}-\x{1F64F}]/u';        $clean_text = preg_replace($regexEmoticons, '', $text);        // Match Miscellaneous Symbols and Pictographs        $regexSymbols = '/[\x{1F300}-\x{1F5FF}]/u';        $clean_text = preg_replace($regexSymbols, '', $clean_text);        // Match Transport And Map Symbols        $regexTransport = '/[\x{1F680}-\x{1F6FF}]/u';        $clean_text = preg_replace($regexTransport, '', $clean_text);        // Match Miscellaneous Symbols        $regexMisc = '/[\x{2600}-\x{26FF}]/u';        $clean_text = preg_replace($regexMisc, '', $clean_text);        // Match Dingbats        $regexDingbats = '/[\x{2700}-\x{27BF}]/u';        $clean_text = preg_replace($regexDingbats, '', $clean_text);        $regexDingbats = '/[\x{231a}-\x{23ab}\x{23e9}-\x{23ec}\x{23f0}-\x{23f3}]/u';        $clean_text = preg_replace($regexDingbats, '', $clean_text);        return $clean_text;    }

The source is here

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.