2013-03-03 53 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實體代碼和特殊字符。