将重音转换为HTML,但忽略标记

时间:2011-01-23 19:45:26

标签: php html encoding

下面的代码转换带重音符号的文字。但它也转换了我想保留的HTML标签。我怎样才能只转换重音字符并保留所有其他特殊字符?感谢。

$temp = file_get_contents("file.html");
echo htmlentities($temp,ENT_NOQUOTES,'UTF-8');

3 个答案:

答案 0 :(得分:22)

htmlspecialchars()htmlspecialchars_decode()仅对&<>'"进行编码/解码;因此,您可以使用后者将其实体转换回HTML特殊字符:

echo htmlspecialchars_decode(htmlentities($temp, ENT_NOQUOTES, 'UTF-8'), ENT_NOQUOTES);

答案 1 :(得分:1)

但是黑客攻击,但您可以先应用htmlentities(),然后再将其反转为标准xml字符(<>,{{1 {}},&")使用htmlspecialchars_decode()。这将恢复标签。

答案 2 :(得分:0)

这似乎工作正常

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;