Convert accents to HTML, but ignore tags
The code below converts text for characters with accents. But it also converts the HTML tags which I would like to leave intact. How can I only convert accented characters and leave all other special characters intact? Thanks.
$temp = file_get_contents("file.html"开发者_如何学C);
echo htmlentities($temp,ENT_NOQUOTES,'UTF-8');
htmlspecialchars()
and htmlspecialchars_decode()
will only encode/decode &
, <
, >
, '
and "
; you could thus use the latter to convert their entities back to their HTML special characters:
echo htmlspecialchars_decode(htmlentities($temp, ENT_NOQUOTES, 'UTF-8'), ENT_NOQUOTES);
A but of a hack, but you can apply htmlentities()
like you already do it first, and then reverse it for the standard xml characters (<
,>
,&
,"
,'
) using htmlspecialchars_decode()
. This will restore the tags.
This seems to work OK
if (!function_exists('make_accents')):
function make_accents($string)
{
//$string = "<p>Angoulême</p>";
$trans = get_html_translation_table(HTML_ENTITIES);
//$encoded = "<p>Angoulême</p>";
$encoded = strtr($string, $trans);
//Next two lines put back the < & > tags
$noHTML = str_replace("<", "<", $encoded);
$encoded = str_replace(">", ">", $noHTML);
return $encoded;
}
endif;
精彩评论