简体   繁体   English

PHP:在mysql中搜索字符串中的单词?

[英]PHP: searching for a word in a string in mysql?

I'm trying to search for a word in a string in mysql database using PHP. 我正在尝试使用PHP在mysql数据库中的字符串中搜索单词。

the issue that I am having is that it searches and spits out the result on my PHP page but any word that is close to the searched word is also gets a result on my page. 我遇到的问题是它在我的PHP页面上搜索并吐出结果,但是任何接近搜索单词的单词也会在我的页面上得到结果。

to explain this better, lets say I have a string which is like this: 为了更好地解释这个,让我说我有一个像这样的字符串:

women's clothing

now if i search for a word which is women the results are being displayed on my page correctly But If i search for the word men the same results are being displayed on my page because the word women has the word men in it . 现在,如果我搜索一个women的单词,结果会正确地显示在我的页面上但是如果我搜索单词men ,我的页面上会显示相同的结果, 因为女性这个单词中有单词men

this is my current sql query: 这是我目前的SQL查询:

SELECT id, category_name, image_url FROM data WHERE `category_name` LIKE '%$sub%' GROUP BY category_name

I tried to change '%$sub%' to '$sub' but that doesn't return any results at all! 我试图将'%$sub%'更改为'$sub'但这根本不会返回任何结果!

could someone please advise on this? 有人可以就此提出建议吗?

Edit: 编辑:

the strings in the category_name column vary and they cannot be changed since they are coming from an external API. category_name列中的字符串各不相同,因为它们来自外部API,所以无法更改。

these strings could vary. 这些字符串可以变化。 so one might look like Women's Dresses & Skirts and another one might look like Women's shoes and another one might look Women's bags and another one might look like men's shoes etc etc.... 所以一个人可能看起来像女式连衣裙和裙子,另一个可能看起来像女鞋,另一个可能看起来像女式皮包,另一个可能看起来像男鞋等等....

so what i need to do is to display all the results for the keyword women and display the results for all the men. 所以我需要做的是显示关键字女性的所有结果,并显示所有男性的结果。 I hope that makes sense. 我希望这是有道理的。

but at the moment, the results of men and women being displayed at the same time because the word Women has the word Men in it as I explained above. 但是目前,男人和女人的结果同时显示,因为正如我在上面解释的那样,女人这个词有男人这个词。

您可以使用MySQL的REGEXP运算符,使用单词匹配来完成此操作。

SELECT ... WHERE column REGEXP '[[:<:]]$sub[[:>:]]' ...

The thing with what you want, is that you want men's clothing to show up but nothing that has something like women. 你想要的东西,就是你想要男装出现,但没有像女人那样的东西。 The only difference between these 2 results is that woMEN has characters in front of it and MEN's clothing has characters after. 这两个结果之间的唯一区别是,woMEN在它前面有字符,而MEN的服装后面有字符。

Mrun his idea is if you want to search for the exact word followed by nothing. Mrun他的想法是,如果你想搜索确切的单词后面没有任何东西。 So when you use: LIKE '% $sub %' only results that have the word MEN followed by nothing and nothing in front of the word MEN directly. 因此,当您使用: LIKE '% $sub %'只有结果显示单词MEN后面没有任何内容,而且直接单词MEN前面没有任何内容。 There has to be a space in between. 中间必须有一个空间。

Now for your result to work, you can use: LIKE $sub% (without the space at the end), however this does mean the word MEN can be followed by anthing. 现在为了使你的结果有效,你可以使用: LIKE $sub% (最后没有空格),但这确实意味着单词MEN可以跟随anthing。 So for example the word MENACE would show up. 例如,MENACE这个词会出现。 If you want specific characters only allowed like @mrun suggested, you can add anything between $sub and the % sign. 如果您想要仅允许特定字符,如@mrun建议,您可以在$sub%符号之间添加任何内容。 You'd have to escape it though . 你必须逃避它

EDIT: 编辑:

Now that I think of it, you'd probably want MENS to be allowed as well but that means you'll have to choose your own conditions cause there's no way for MYSQL to automatically detect that the plural form of a word is allowed as well etc. 现在我想起来了,你可能也希望允许MENS,但这意味着你必须选择自己的条件,因为MYSQL无法自动检测到允许复数形式的单词也是如此等等

There are several options for this. 有几种选择。 Here's one of them: 这是其中之一:

SELECT id, category_name, image_url FROM data WHERE `category_name` LIKE '% $sub %' OR `category_name` LIKE '% $sub' OR `category_name` LIKE '$sub' OR `category_name` LIKE '$sub %' OR `category_name` LIKE '% $sub\' %' OR `category_name` LIKE '% $sub' OR `category_name` LIKE '$sub\'' OR `category_name` LIKE '$sub\' %' GROUP BY category_name

Another way to do this is to use the MATCH AGAINST, but you will need to use the FULLTEXT index, here's how: 另一种方法是使用MATCH AGAINST,但你需要使用FULLTEXT索引,方法如下:

SELECT id, category_name, image_url FROM data WHERE MATCH (`category_name`) AGAINST ('+$sub' IN BOOLEAN MODE) GROUP BY category_name

okay guys, thanks for all your inputs. 好的伙计们,感谢您的所有投入。 this is how I did it and its working now: 这就是我做到这一点及其现在的工作方式:

SELECT id, category_name, aw_image_url FROM data WHERE category_name RLIKE '[[:<:]]".$sub."[[:>:]]' GROUP BY category_name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM