How PHP determines emoji

Source: Internet
Author: User
iOS input emoji MySQL storage not (online can not change utf8mb4), how to determine the input character contains emoji, and then prompt

Reply content:

iOS input emoji MySQL storage not (online can not change utf8mb4), how to determine the input character contains emoji, and then prompt

Do not determine whether it is emoji, the transmission of the Json_encode encrypted after the database, and then take out the time with Json_decode to show it, Json_decode can also urlencode a bit before, Take out the time to urldecode a bit;

Before storing the database

$message = json_encode($message);

After removal

$message = json_decode($message)

Code

$regex = '/([0-9|#][\x{20E3}])|[\x{00ae}|\x{00a9}|\x{203C}|\x{2047}|\x{2048}|\x{2049}|\x{3030}|\x{303D}|\x{2139}|\x{2122}|\x{3297}|\x{3299}][\x{FE00}-\x{FEFF}]?|[\x{2190}-\x{21FF}][\x{FE00}-\x{FEFF}]?|[\x{2300}-\x{23FF}][\x{FE00}-\x{FEFF}]?|[\x{2460}-\x{24FF}][\x{FE00}-\x{FEFF}]?|[\x{25A0}-\x{25FF}][\x{FE00}-\x{FEFF}]?|[\x{2600}-\x{27BF}][\x{FE00}-\x{FEFF}]?|[\x{2900}-\x{297F}][\x{FE00}-\x{FEFF}]?|[\x{2B00}-\x{2BF0}][\x{FE00}-\x{FEFF}]?|[\x{1F000}-\x{1F6FF}][\x{FE00}-\x{FEFF}]?/u';var_dump(preg_match($regex, '?'));var_dump(preg_match($regex, '?'));var_dump(preg_match($regex, '?'));var_dump(preg_match($regex, '测试'));var_dump(preg_match($regex, 'hello, world'));var_dump(preg_match($regex, 'testing'));var_dump(preg_match($regex, '中文 English'));

Result

int(1)int(1)int(1)int(0)int(0)int(0)int(0)

The expression is found, and you can look at the range of emoji in the UTF8 's Code table (or go UTF8 from the Unicode Code table range)

But suggest or change utf8mb4, this kind of migration basically is painless, full test can

The fundamental solution is that the database supports emoji

Are you sure you don't put emoji in the database, but you detect and then prompt?

Detection

//返回true就表示有emojifunction testEmoji($str){    $text = json_encode($str); //暴露出unicode    return preg_match("/(\\\u[ed][0-9a-f]{3})/i",$text);}

Escaping and depositing into the database

Look at my old problems, http://segmentfault.com/q/1010000003711491.

  • Related Article

    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.