What are the potential pitfalls of using str_replace to replace characters with images in PHP?

Using str_replace to replace characters with images in PHP can be problematic because it can potentially replace parts of HTML tags or attributes, leading to broken markup. To avoid this issue, it's better to use a more robust method like preg_replace with a regular expression that specifically targets the text you want to replace.

// Example of using preg_replace to replace characters with images in a safer way
$text = "Replace this text with images";
$replaced_text = preg_replace('/text/', '<img src="image.jpg" alt="text">', $text);
echo $replaced_text;