What are the potential pitfalls of using htmlentities with UTF-8 encoding?

Using htmlentities with UTF-8 encoding can potentially lead to double-encoding issues, where characters are encoded twice and displayed incorrectly on the webpage. To solve this issue, it is recommended to use htmlspecialchars instead of htmlentities when dealing with UTF-8 encoded strings in PHP.

// Using htmlspecialchars instead of htmlentities to avoid double-encoding issues with UTF-8
$utf8_string = "Café";
$encoded_string = htmlspecialchars($utf8_string, ENT_QUOTES, 'UTF-8');
echo $encoded_string;