特殊字符我有特殊字符,如E,O和許多描述更多並試圖用其刪除:刪除在PHP
$string = str_replace("é", " ", $string)
$string = ereg_replace("é", " ", $string)
$string = mysql_real_escape_string($string)
但沒有任何工程,特殊字符仍然存在,並說明沒有插入到數據庫中。我無法刪除所有特殊字符,因爲在描述中有需要的html標籤。
謝謝你的幫助!
特殊字符我有特殊字符,如E,O和許多描述更多並試圖用其刪除:刪除在PHP
$string = str_replace("é", " ", $string)
$string = ereg_replace("é", " ", $string)
$string = mysql_real_escape_string($string)
但沒有任何工程,特殊字符仍然存在,並說明沒有插入到數據庫中。我無法刪除所有特殊字符,因爲在描述中有需要的html標籤。
謝謝你的幫助!
易peasy:
function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}
用法:
echo clean('a|"[email protected]£de^&$f g');
將輸出:abcdef-g
編輯:
Hey, just a quick question, how can I prevent multiple hyphens from being next to each other? and have them replaced with just 1? Thanks in advance!
function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
return preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.
}
http://www.php.net/manual/en/function.str-replace.php
該函數返回字符串或與替換值的數組。
您需要使用返回值
$string = str_replace("é", " ", $string);
對不起,我知道,只要忘記寫在我的問題中。 – Marko
實施例:
<?php
$string = "ʿABBĀSĀBĀD";
echo iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $string);
// output: ABBASABAD
?>
嘗試過,返回false – Marko
[_Cannot reproduce_(https://eval.in/127631) –
一切都UTF8?這聽起來像你有混合的字符集。 – h2ooooooo
我得到的數據庫表格行設置爲utf8_bin – Marko