2012-06-27 41 views
0

我正在嘗試改進使用PHP的搜索結果。例如當產品名稱是ZSX-3185BC用戶不能找到產品時,把類似ZSX3185BC。到目前爲止,我得到的東西,如:通過擴大匹配來改進搜索結果

$input = '01.10-2010'; 
$to_replace = array('.','-'); 
$clean = str_replace($to_replace, '/',$input);` 

我已經試過這一點,但它不能正常工作(不知道,張貼在評論沒有闡述-ed)

$str = ""; 
$len = strlen($strSearch) - 1; 
// Loop through the search string to check each charecter is acceptable and strip those that are not 
for ($i = 0; $i <= $len; $i++) { 
    if(preg_match("/\w|-| /",$strSearch[$i])) { 
     $str = $str . $strSearch[$i]; 
    } 
} 
+1

你想在mysql查詢中直接替換特殊字符的搜索結果嗎? –

+0

顯示一些確切的代碼,你的嘗試。 –

+0

'$ str =「」; $ len = strlen($ strSearch) - 1; //循環通過搜索字符串來檢查每個性格特徵是可接受的,並且剝去那些不 \t對於($ i = 0; $ I <= $ LEN; $ I ++) \t { \t \t如果(的preg_match(」/\ w | - | /「,$ strSearch [$ i])) \t \t { \t \t \t $ str = $ str。 $ strSearch [$ i]於; \t \t} \t}' –

回答

-1

試試這個:

 
SELECT * FROM tablename WHERE replace(fieldname, '-', '') like '%ZSX3185BC%'; 
+0

這將強制執行全表掃描,並且不能使用任何索引。 –

+0

而'SELECT *'是不好的做法 - 指定列。我知道你不知道在這種情況下的列(不提供),但只是不輸入明星! –

+0

我剛剛舉了一個例子,他能夠指定他想要的列。 –

相關問題