php extract Emoji from a string
Asked Answered
P

1

7

I have a string contain emoji. I want extract emoji's from that string,i'm using below code but it doesn't what i 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 also if:

$string = "πŸ˜…πŸ˜‡β˜πŸΏ"

it return only first emoji

$emoji = ["πŸ˜…"]
Physicist answered 25/7, 2017 at 14:27 Comment(1)
try preg_match_all() – Rudolf
R
11

Try looking at preg_match_all function. preg_match stops looking after it finds the first match, which is why you're only ever getting the first emoji back.

Taken from this answer:

preg_match stops looking after the first match. preg_match_all, on the other hand, continues to look until it finishes processing the entire string. Once match is found, it uses the remainder of the string to try and apply another match.

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

So your code would 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] => πŸ™ƒ ) 
Rosamondrosamund answered 25/7, 2017 at 14:40 Comment(2)
you can get the regular expression that supports new emojis from this link – Showmanship
Updated namal's link to include some Unicode 12.0 emojis including newest emoji, white heart. link – Yee

© 2022 - 2024 β€” McMap. All rights reserved.