我需要一個函數來清理字符串的特殊字符。我不想這HTML字符轉換像 <br />
到 <br />
使用php將特殊字符轉換爲html代碼
我想要的東西轉換,如:•1/2,」爲HTML代碼。
這是我目前使用的功能,但它不會出現與分數來工作..
function cleanText($str){
$str = str_replace("Ñ" ,"Ñ", $str);
$str = str_replace("ñ" ,"ñ", $str);
$str = str_replace("ñ" ,"ñ", $str);
$str = str_replace("Á","Á", $str);
$str = str_replace("á","á", $str);
$str = str_replace("É","É", $str);
$str = str_replace("é","é", $str);
$str = str_replace("ú","ú", $str);
$str = str_replace("ù","ù", $str);
$str = str_replace("Í","Í", $str);
$str = str_replace("í","í", $str);
$str = str_replace("Ó","Ó", $str);
$str = str_replace("ó","ó", $str);
$str = str_replace("「","“", $str);
$str = str_replace("」","”", $str);
$str = str_replace("‘","‘", $str);
$str = str_replace("’","’", $str);
$str = str_replace("—","—", $str);
$str = str_replace("–","–", $str);
$str = str_replace("™","™", $str);
$str = str_replace("ü","ü", $str);
$str = str_replace("Ü","Ü", $str);
$str = str_replace("Ê","Ê", $str);
$str = str_replace("ê","î", $str);
$str = str_replace("Ç","Ç", $str);
$str = str_replace("ç","ç", $str);
$str = str_replace("È","È", $str);
$str = str_replace("è","è", $str);
$str = str_replace("•","•" , $str);
$str = str_replace("¼","¼" , $str);
$str = str_replace("½","½" , $str);
$str = str_replace("¾","¾" , $str);
$str = str_replace("½","½" , $str);
return $str;
}
如果$ str中的數據編碼在PHP文件所在的不同編碼和/或代碼頁中,則該字符將不匹配。你可以很容易地檢查:如果是這種情況,'echo「½」;'應該在瀏覽器中顯示爲不同的字符。 – GolezTrol
*「我想將諸如'''','½','''轉換爲html代碼」* - 爲什麼?這不應該是必要的。 – Tomalak
當你在一個地方看到很多'str_replace's時,你應該認爲這可能是錯誤的。 'utm-8'中的'htmlentities'爲 –