What are common pitfalls when trying to convert smilies in PHP?

Common pitfalls when trying to convert smilies in PHP include not properly escaping special characters in the smilies array, not using a regular expression with word boundaries to avoid partial replacements, and not handling cases where smilies are part of a larger word. To solve this, make sure to escape special characters in the smilies array, use word boundaries in the regular expression pattern, and consider using a callback function to handle more complex replacements.

$smilies = array(
    ':)' => '<img src="smile.png" alt=":)" />',
    ':(' => '<img src="sad.png" alt=":(" />'
);

function convert_smilies($text, $smilies) {
    $pattern = '/\b(' . implode('|', array_map('preg_quote', array_keys($smilies))) . ')\b/';
    return preg_replace_callback($pattern, function($match) use ($smilies) {
        return $smilies[$match[0]];
    }, $text);
}

$text = 'Hello, I am feeling :) today!';
echo convert_smilies($text, $smilies);