How can you efficiently loop through an array of smiley codes and corresponding image paths to replace them in a text string?

To efficiently loop through an array of smiley codes and corresponding image paths to replace them in a text string, you can use the str_replace function in PHP. First, create an array where the keys are the smiley codes and the values are the corresponding image paths. Then, loop through this array and use str_replace to replace each smiley code with its corresponding image path in the text string.

// Array of smiley codes and corresponding image paths
$smileyArray = array(
    ':)' => 'smiley1.png',
    ':D' => 'smiley2.png',
    ':P' => 'smiley3.png'
);

// Text string with smiley codes
$text = 'Hello :) How are you? :D';

// Loop through the smiley array and replace codes with image paths in the text string
foreach ($smileyArray as $code => $image) {
    $text = str_replace($code, '<img src="' . $image . '">', $text);
}

echo $text;