I have foreign language strings stored in utf8. When displaying them, I want them to be transform in such a way that accented chars become their html counterparts, e.g. é
becomes é
However I phrase my search, all I can find is htmlentities()
which does not do it.
I found this is a very simple solution to this problem.
The first part tries to encode the text. If the $output returns a blank string, then the value is utf8 encoded and then the html entities are created.
$output = htmlentities($value, 0, "UTF-8");
if ($output == "") {
$output = htmlentities(utf8_encode($value), 0, "UTF-8");
}
Example:
Montréal
Output:
Montréal
Make sure you're properly specifying the encoding when you call htmlentities()
.
The documentation shows that it defaults to 'UTF-8'
, but if you read down a bit further, you'll see that that's new as of PHP 5.4. If you're using an older version, the default is actually 'ISO-8859-1'
, and you'll need to make sure you explicitly specify it as 'UTF-8'
instead of relying on the default behaviour.
htmlentities($string, 0, 'UTF-8');
This was posted as a comment on another answer, but was exactly what I needed so I'm posting it as an answer on its own:
There are cases when you want to generate entities for accented characters (à ,è, ì, ò, ù, ...) but want to preserve HTML code (so do not excape "<" and ">" and avoid escaping already escaped entities. In those cases, you can use this code:
$string = str_replace(array("<", ">"), array("<", ">"), htmlentities($string, ENT_NOQUOTES, 'UTF-8', FALSE));
This code is compatible with PHP >= 5.2.3
(source)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With