Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert accented chars to html in php?

Tags:

html

php

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.

like image 767
Gabriel Avatar asked Jul 12 '13 22:07

Gabriel


3 Answers

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
like image 176
Peter F Avatar answered Nov 03 '22 17:11

Peter F


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');
like image 45
jcsanyi Avatar answered Nov 03 '22 18:11

jcsanyi


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("&lt;", "&gt;"), array("<", ">"), htmlentities($string, ENT_NOQUOTES, 'UTF-8', FALSE)); 

This code is compatible with PHP >= 5.2.3

(source)

like image 35
That Brazilian Guy Avatar answered Nov 03 '22 18:11

That Brazilian Guy