2013-07-01 55 views
0

我有一串數據,我剝離了一些單詞,然後放入一個數組放入查詢。str_replace和like查詢

這個詞似乎很好剝奪,但類似的聲明,它仍然在拾起它們,並插入一個空白的搜索。這是在拖回所有的東西,這是不好的。

我不太確定爲什麼這樣做。我看起來不像是在單詞之間增加了一個額外的空格,而且我已經嘗試打印出數組,並且沒有看起來關閉,所以我不知所措。

這是我str_replace函數

$str = str_replace(array('and', 'this', 'that', 'or', 'boo', 'wound', 'but'), '', $userPost); 

,我再在空間爆炸把它變成一個數組

$words = explode(' ', $str); 

,然後在foreach我做我喜歡的語句

$this->db->or_where("`terms` LIKE '%$word%'"); 

和查詢是這樣出來的

SELECT * FROM (`filter_tbl`) WHERE `terms` LIKE '%real%' OR `terms` LIKE '%%' OR `terms` LIKE '%blank%' OR `terms` LIKE '%%' OR `terms` LIKE '%%' OR `terms` LIKE '%%' OR `terms` LIKE '%%' OR `terms` LIKE '%I%' OR `terms` LIKE '%love%' OR `terms` LIKE '%%' OR `terms` LIKE '%ty%' OR `terms` LIKE '%lets%' OR `terms` LIKE '%see%' OR `terms` LIKE '%if%' OR `terms` LIKE '%%' OR `terms` LIKE '%goes%' OR `terms` LIKE '%through%' OR `terms` LIKE '%please%' OR `terms` LIKE '%gg%' OR `terms` LIKE '%through%' OR `terms` LIKE '%%' OR `terms` LIKE '%%' OR `terms` LIKE '%%' 

我知道這可能是簡單的東西我很想念,但我只是看不到它。幫助表示讚賞!

+0

使用全文搜索替代 –

+0

我正在使用全文,我只是想省略一些不必要的詞語 – zazvorniki

+0

那麼你的查詢是錯誤的。 –

回答

1

正如人們在上面發佈的那樣,您的查詢對於全文索引是不正確的。一堆OR子句通常會起作用,但可能不是很高效。

如果你想擺脫多餘的空格在您輸入的結束,你可能只是需要從陣列修剪多餘的空格

$words = explode(' ', $str); // from your code 
$words = array_map('trim',$words); // trim any extra whitespace from the words array 
$words = array_filter($words); // remove any blank elements 

這應該擺脫任何多餘項的OR條款。