regex - php extract Emoji from a string -


i have string contain emoji. want extract emoji's string,i'm using below code doesn't want.

$string = "😃 hello world 🙃";  preg_match('/([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', $string, $emojis); 

i want this:

$emojis =  ["😃", "🙃"]; 

but return this:

$emojis = ["😃"] 

and if:

$string = "😅😇☝🏿" 

it return first emoji

$emoji = ["😅"] 

try looking @ preg_match_all function. preg_match stops looking after finds first match, why you're ever getting first emoji back.

taken this answer:

preg_match stops looking after first match. preg_match_all, on other hand, continues until finishes processing entire string. once match found, uses remainder of string try , apply match.

http://php.net/manual/en/function.preg-match-all.php

so code become:

$string = "😃 hello world 🙃";  preg_match_all('/([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', $string, $emojis);  print_r($emojis[0]); // array ( [0] => 😃 [1] => 🙃 )  

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -