php html解码字符串

时间:2013-03-03 08:49:26

标签: php

我有一个数组,它返回各种字符串,我正在尝试html-decode。但我似乎无法找到适用于所有字符串的函数。例如,我的一个字符串看起来像这样:

"a detail of The School of Athens<!-- this should link to an article about the famous artwork -->, a fresco by Raphael"

而另一个看起来像这样:

""Aristotle" by Francesco Hayez (1791–1882)"

我可以使用html_entity_decode删除第一个字符串中的评论(<!--/-->),也可以使用htmlentities来删除–到{ {1}},但我找不到任何可以将我的所有字符串更改为常规文本的内容。有没有可以做到这一点的功能?

TIA!

1 个答案:

答案 0 :(得分:0)

<!doctype html>

<html lang="en">
<head>
    <title>String Cleansing</title>
</head>
<body>
<?php
echo '<pre>';

// The below are the strings
echo $string_with_htmlent =
     "a detail of The School of Athens<!-- this should link to an article about the famous artwork -->, a fresco by Raphael";

echo '<br>';

echo $string_unicode = 
     "Aristotle by Francesco Hayez (1791";
?>&acirc;&euro;&quot;
<?php 

echo $string_unicode_c = "1882)";    
echo '<br>';

// The below is how you fixed
echo $a = html_entity_decode($string_with_htmlent); echo '<br>';

echo $b = htmlentities($string_unicode.$string_unicode_c);

echo '<br>';

// The below is how I code and you expect
$clean = $string_with_htmlent.' '.$string_unicode.' '.$string_unicode_c; 

var_dump(filter_var($clean,FILTER_SANITIZE_STRING));

echo '</pre>';
?>
</body>
</html>

html_entity_decodehtmlentities的替代为FILTER_SANITIZE_STRING。使用一个内置函数修复所有html实体代码和特殊字符。