What are the best practices for using str_replace in PHP to replace text with images like smileys?

When using str_replace in PHP to replace text with images like smileys, it is important to ensure that the replacement text is unique and won't accidentally replace other parts of the text that are not intended to be replaced. One way to do this is by using a unique identifier for each smiley image and replacing it with the corresponding HTML image tag.

// Define an array mapping smiley text to image file paths
$smileys = array(
    ':)' => 'smiley.png',
    ':D' => 'big-smile.png',
    ';)' => 'wink.png'
);

// Loop through the smileys array and replace each smiley text with the corresponding image tag
foreach ($smileys as $smiley => $image) {
    $text = str_replace($smiley, '<img src="images/' . $image . '" alt="' . $smiley . '">', $text);
}

// Output the text with smileys replaced by images
echo $text;