【问题标题】:str_replace and like querystr_replace 和类似查询
【发布时间】:2013-07-01 22:42:29
【问题描述】:

我有一个数据字符串,我从中剥离了一些单词,然后将其放入一个数组中以放入查询中。

这些词似乎可以很好地剥离,但 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 '%%' 

我知道这可能是我缺少的一些简单的东西,但我就是看不到它。感谢您的帮助!

【问题讨论】:

  • 改用全文搜索
  • 我用的是全文,只是想省略一些不必要的词
  • 那么你的查询是错误的。
  • 我的查询工作正常,我只是想省略一些要搜索的单词,因为它来自文本区域。
  • 您的查询对于全文类型搜索不正确 .. MATCH (XX) AGAINST ... dev.mysql.com/doc/refman/5.0/en/fulltext-search.html

标签: php sql codeigniter str-replace


【解决方案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 子句中的任何无关条目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多