1

My objective is to check if the message have emojis or not , if yes i am going to convert them. I am having a problem to find out how many emojis are in the string.

Example :

$string = ":+1::+1::+1:"; //can be any string that has :something: between ::
  • the objective here is to get the :+1: ( all of them ) but i tried two pattern and i keep getting only 1 match.

    preg_match_all('/:(.*?):/', $string, $emojis, PREG_SET_ORDER); // $emojis is declared as $emojis='' at the beginning.
    preg_match_all('/:(\S+):/', $string, $emojis, PREG_SET_ORDER);
    

The Rest of the code
after getting the matches i am doing this :

 if (isset($emojis[0])) 
   {
        $stringMap = file_get_contents("/api/common/emoji-map.json");
        $jsonMap = json_decode($stringMap, true);

        foreach ($emojis as $key => $value) {
            $emojiColon = $value[0];
            $emoji = key_exists($emojiColon, $jsonMap) ? $jsonMap[$emojiColon] : '';
            $newBody = str_replace($emojiColon, $emoji, $newBody);
        }
   }

i would appreciate any help or suggestions , Thanks.

2
  • 4
    I get all 3 matches with preg_match_all 3v4l.org/DLsmq Commented Jan 16, 2020 at 19:23
  • 1
    i tried that in different cases but i still have some problems , i'll test more tho , thank you Commented Jan 16, 2020 at 19:25

1 Answer 1

1

I updated your expression a little bit:

preg_match_all('/\:(.+?)(\:)/', $string, $emojis, PREG_SET_ORDER);
echo var_dump($emojis);    
  • The : is now escaped, otherwise it could be treated as special character.
  • Replaced the * by +, this way you won't match two consecutive :: but require at least one charcter in-between.
Sign up to request clarification or add additional context in comments.

1 Comment

Hello bulletvz, welcome to Stackoverflow! Your answer has been flagged because of its short length. For better readable answers next time, please take your time and provide a short comment of what you did to fix the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.