What are some common methods for replacing text with images (smilies) in PHP?

When working with text in PHP, a common task is to replace certain text patterns with images, such as smiley faces or emoticons. One way to achieve this is by using the str_replace() function to search for specific text patterns and replace them with the corresponding image tags.

$text = "Hello :) How are you?";
$smilies = array(
    ":)" => "<img src='smiley.png' alt=':)' />",
    ":(" => "<img src='sad.png' alt=':(' />"
);

foreach($smilies as $smiley => $image) {
    $text = str_replace($smiley, $image, $text);
}

echo $text;