开发者

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 = "&lt;p&gt;Angoul&ecirc;me&lt;/p&gt;";
    $encoded = strtr($string, $trans);
    //Next two lines put back the < & > tags
        $noHTML = str_replace("&lt;", "<", $encoded);
    $encoded = str_replace("&gt;", ">", $noHTML);
    return $encoded;
}
endif;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜