2013-03-03 56 views
1

我有一个数组,它返回各种字符串,我试图html解码。但我似乎无法找到适用于所有字符串的函数。例如,我的一个字符串看起来是这样的:PHP的html解码字符串

"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在第二个将–更改为-,但我找不到任何将所有我的字符串更改为常规文本。有没有可以做到这一点的功能?

TIA!

回答

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_decodehtmlentitiesFILTER_SANITIZE_STRING。用一个内置函数修复所有html实体代码和特殊字符。