开发者

Php search string without allowing substrings

I want to search this string: "This is, mybigstring"

And search words could be: "is, big, mybigstring"

At the moment I'm using stristr which will find matches for all these words. However I do not want "big" to match.. Meaning that I won't allow substrings. How can I do this?

Update: I tried this out after reading your answers:

$testregxp = "\b(copenhagen|kobenhavn)\b";
$test = "C开发者_如何学Pythonopenhagen, Denmark";
print preg_match($testregxp, strtolower($test));

But I can't seem to get it working..


You could use a regular expression for this. Depends if you want to find all instances of the word.

http://php.net/manual/en/function.preg-match.php

or

http://www.php.net/manual/en/function.preg-match-all.php to find all instances instead of just 1 match.

Also if you want to search all of them at the same time you can do that as well.

Something like

\bis\b should work. this should search for the actual word "is", so tis would not match, but your case "is," will even with that comma there.

to search for any of them at the same time you can do

\b(is|This|big)\b this will match is and This, but big won't be found.


Update

I'm not exactly sure what you want 100%, but you can just add the comma. IE: "/\b(copenhagen|kobenhavn)\b,/i" now if you want to include the comma in the search you can do, "/\b(copenhagen|kobenhavn)\b,?/i", so the comma is optional. If you did

\b(copenhagen|kobenhavn|denmark)\b,?

on

"Copenhagen, Denmark"

it will match

Copenhagen,

and

Denmark

Note the comma in Copenhagen, but the comma is optional in this case. So it will match the word with or without it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜