2013-07-21 35 views
0

所以我製作了這個刮刀,它從多個站點返回字符串。我想檢查字符串是否匹配,所以我使用php來清理字符串並檢查。但是,&和其他特殊字符有兩種出現,一種是&,另一種是&。我如何去除每種類型。PHP清理字符串中的特殊字符

preg_replace("/[^a-zA-Z0-9]+/", "", $string); 

我已經有了,但並沒有拿出特殊字符。

謝謝。

+0

你想顯示和放大器AS&?或&as& – Dolchio

+0

一個顯示爲&,另一個爲& – Username

+1

查看我的回答。你必須使用[html_entity_decode](http://php.net/manual/en/function.html-entity-decode.php) –

回答

1

試試這個

function removeSpecialChar($string) { 
    $string = str_replace('', '-', $string); // Replaces all spaces with hyphens. 
    return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars. 
} 

echo removeSpecialChar(html_entity_decode('&khawer&')); //will output khawer 
+0

謝謝,解決了它。 html_entity_decode是我正在尋找的。再次感謝。 – Username

+0

很高興幫助你:) –

1

我認爲你正在尋找htmlspecialchars()函數。

通過此運行您的字符串,以及strip_tags()和strip_slashes()應清理您的字符串。

htmlspecialchars(strip_tags(strip_slashes($string)));

+0

當我運行這一個&變成& – Username