How can PHP code be structured to ensure that replaced smileys in text are displayed correctly in emails and other output formats?

When replacing smileys in text with corresponding emoticons in PHP, make sure to use HTML entities for the emoticons to ensure they are displayed correctly in emails and other output formats. This can be achieved by converting the emoticons to their respective HTML entities before outputting the text.

$text = "Hello :) This is a smiley face.";
$smileys = array(
    ':)' => '😀', // Unicode for smiley face emoticon
    // Add more smiley replacements as needed
);

foreach ($smileys as $smiley => $entity) {
    $text = str_replace($smiley, $entity, $text);
}

echo $text;