首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >str_replace和like查询

问str_replace和like查询
EN

Stack Overflow用户
提问于 2013-07-02 06:42:29
回答 1查看 550关注 0票数 0

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

单词似乎被剥离得很好,但like语句仍然拾取它们所在的位置,并插入一个空白搜索。那就是把所有的东西都拉回来,这可不好。

我真的不确定为什么要这样做。我看起来没有在单词之间添加额外的空格,并且我尝试打印出数组,但没有任何异常,所以我感到困惑。

这是我的str_replace

代码语言:javascript
复制
$str = str_replace(array('and', 'this', 'that', 'or', 'boo', 'wound', 'but'), '', $userPost);

然后我在空间中爆炸,把它变成一个数组

代码语言:javascript
复制
$words = explode(' ', $str);

然后在foreach中我正在做我的like语句

代码语言:javascript
复制
 $this->db->or_where("`terms` LIKE '%$word%'");

查询结果如下所示

代码语言:javascript
复制
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 '%%' 

我知道这可能是我遗漏了一些简单的东西,但我就是看不出来。感谢您的帮助!

EN

回答 1

Stack Overflow用户

发布于 2013-07-02 06:59:43

正如人们在上面发布的,您的查询对于全文索引是不正确的。一堆OR子句通常可以工作,但性能可能不是很好。

如果您想去掉输入末尾的多余空格,您可能只需要从数组中去掉多余的空格

代码语言:javascript
复制
$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子句中的任何无关条目。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17414832

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档