How can text smilies be replaced with image smilies in PHP?

To replace text smilies with image smilies in PHP, you can use a simple str_replace function to replace the text smilies with HTML img tags pointing to the corresponding image files. You can create an array mapping text smilies to image file paths, and then loop through your text content to replace the text smilies with image tags.

<?php
// Array mapping text smilies to image file paths
$smilies = array(
    ':)' => 'smile.png',
    ':(' => 'sad.png',
    ':D' => 'laugh.png'
);

// Sample text content with text smilies
$text = "Hello, how are you? :) I'm feeling happy!";

// Loop through the smilies array and replace text smilies with image tags
foreach($smilies as $text_smiley => $image_path) {
    $text = str_replace($text_smiley, '<img src="' . $image_path . '">', $text);
}

// Output the text content with image smilies
echo $text;
?>