Convert Accented Characters to HTML Entities in PHP
- 1 minute read
This PHP function uses the built-in htmlentities function to convert the accented characters in a given string to HTML entities:
function accentedToHtml($str) {
$html = htmlentities($str, 0, 'UTF-8');
if ($html === '') {
$html = htmlentities(utf8_encode($str), 0, 'UTF-8');
}
return $html;
}
Here is some example usage:
$str = "éxample";
$html = accentedToHtml($str);
After the above code has run, $html
will equal éexample
.
Conclusion
I hope you found this useful!