Match and replace emoticons in string - what is the most efficient way?
Wikipedia defines a lot of possible emoticons people can use. I want to match this list to words in a string. I now have this: $string = "Lorem ipsum :-) dolor :-| samet"; $emoticons = array( '[HAPPY]' => array(' :-) ', ' :) ', ' :o) '), //etc... '[SAD]' => array(' :-( ', ' :( ', ' :-| ') ); foreach ($emoticons as $emotion => $icons) { $string = str_replace($icons, " $emotion ", $string); } echo $string; Output: Lorem ipsum [HAPPY] dolor [SAD] samet so in principle this works. However, I have two questions: As you can see, I'm putting spaces around each emoticon in the array, such as ' :-) '