How can PHP be used to replace specific words in the output with images?

To replace specific words in the output with images using PHP, you can create an associative array where the keys are the words to be replaced and the values are the paths to the corresponding images. Then, you can use the str_replace function to replace the words with the image tags in the output.

<?php
// Define an associative array with words to be replaced and corresponding image paths
$word_image_map = array(
    'apple' => 'images/apple.jpg',
    'banana' => 'images/banana.jpg',
    'orange' => 'images/orange.jpg'
);

// Sample output text
$output_text = 'I like apple and banana but not orange.';

// Replace words with images in the output
foreach ($word_image_map as $word => $image) {
    $output_text = str_replace($word, '<img src="' . $image . '" alt="' . $word . '">', $output_text);
}

echo $output_text;
?>