Home  >  Article  >  Backend Development  >  javascript - How to tell if a string contains emoji characters?

javascript - How to tell if a string contains emoji characters?

WBOY
WBOYOriginal
2016-08-18 09:16:352820browse

I made a page, mainly the content entered on the mobile phone, to be displayed on the PC browser.

However, emoji can be input on the mobile phone, but the browser cannot display it. For some reason I don't want it to show up either.

So I hope to do a test when submitting on the mobile phone to check whether the text entered by the user contains emoji. If so, the user will be prompted not to include emoji.

How to write this if condition?

php backend or js frontend code is available.

Reply content:

I made a page, mainly the content entered on the mobile phone, to be displayed on the PC browser.

However, emoji can be input on the mobile phone, but the browser cannot display it. Don't want it to show up either for some reason.

So I hope to do a test when submitting on the mobile phone to check whether the text entered by the user contains emoji. If so, the user will be prompted not to include emoji.

How to write this if condition?

php backend or js frontend code is available.

Use regular expressions to filter, or use unicode encoding to determine

http://www.unicode.org/Public...
Filter according to the range listed inside. You can use regular expressions or list a hash map

var re = /[expression 1]|[expression 2]|[expression 3]..../ ;
re.test (the character you want to detect);

Actually there is a way. Use the mb series of functions to achieve this. First determine the length. Then, loop to determine each character, which character is 4 bytes. One emoiji symbol takes up 4 characters. General strings, that is, Chinese characters, can occupy up to 3 characters. Once you make this judgment, it will be settled.

Determine the character encoding, then match and replace.

<code>var param = "";
var regRule = /\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g;
if(param.match(regRule)) {
    param = param.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g, "");
    alert("不支持表情");
} </code>

<code>function have_emoji($str){
    $mat = [];
    preg_match_all('/./u', $str,$mat);
    foreach ($mat[0] as $v){
        if(strlen($v) > 3){return true;}
    }
    return false;
}</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn