2014-03-27 52 views
1

特殊字符我有特殊字符,如E,O和許多描述更多並試圖用其刪除:刪除在PHP

$string = str_replace("é", " ", $string) 
$string = ereg_replace("é", " ", $string) 
$string = mysql_real_escape_string($string) 

但沒有任何工程,特殊字符仍然存在,並說明沒有插入到數據庫中。我無法刪除所有特殊字符,因爲在描述中有需要的html標籤。

謝謝你的幫助!

+1

[_Cannot reproduce_(https://eval.in/127631) –

+0

一切都UTF8?這聽起來像你有混合的字符集。 – h2ooooooo

+0

我得到的數據庫表格行設置爲utf8_bin – Marko

回答

0

易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. 
} 

refer this link

+0

有一刻,所以我可以試試 – Marko

+0

@ Never84,看看我的回答與參考鏈接... – jmail

+0

它刪除所有html凝視結束標籤符號,我註釋掉部分刪除空格,因爲我需要他們,我需要字符like()<>/ – Marko

0

使用character transliteration

實施例:

<?php 
$string = "ʿABBĀSĀBĀD"; 

echo iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $string); 
// output: ABBASABAD 

?> 
+0

嘗試過,返回false – Marko

0
mysql_real_escape_string(htmlentities($string)); 
+0

返回一個長度爲0的字符串 – Marko

+0

你有一個db連接嗎?從手冊:MySQL連接。如果未指定鏈接標識符,則假定由mysql_connect()打開的最後一個鏈接。如果沒有找到這樣的鏈接,它會嘗試創建一個,就好像調用mysql_connect()時沒有參數一樣。如果未找到或建立連接,則會生成E_WARNING級別錯誤。 – Napolux