How can PHP be used to dynamically replace text with images (like smilies) in a guestbook entry?
To dynamically replace text with images (like smilies) in a guestbook entry using PHP, you can create an array that maps text emoticons to their corresponding image URLs. Then, use the str_replace function to replace the text emoticons with the corresponding image tags in the guestbook entry.
<?php
// Define an array mapping text emoticons to image URLs
$smilies = array(
':)' => 'smile.png',
':(' => 'sad.png',
':D' => 'laugh.png'
);
// Guestbook entry with text emoticons
$guestbookEntry = "Hello! :)";
// Replace text emoticons with corresponding image tags
foreach ($smilies as $text => $image) {
$guestbookEntry = str_replace($text, '<img src="' . $image . '" alt="' . $text . '">', $guestbookEntry);
}
// Display the guestbook entry with replaced images
echo $guestbookEntry;
?>