How can HTML entities be handled in PHP when replacing characters?

When replacing characters in PHP, HTML entities need to be handled properly to ensure that they are not accidentally converted or removed. One way to handle HTML entities is to use the `htmlspecialchars_decode()` function in PHP. This function will convert HTML entities back to their corresponding characters before performing any replacements.

// Example code snippet to handle HTML entities when replacing characters in PHP
$string = "This is an example & text with HTML entities.";
$replacedString = str_replace('&', '&', htmlspecialchars_decode($string));

echo $replacedString;