Jump to content

Making strpos() return exact matches...


OsvaldoM

Recommended Posts

Hello,

i am using strpos for quick searches in large texts, everything was perfect till i found out strpos return the ocurrences for the string in-spite of the fact that is the whole word or just a part of it, and i need only to return exact matches...

To exemplify what i am trying to say: say, i use strpos to look for "wonder" in a string, i would like it to return:

 

I wonder if...

I like Wonder bread...

 

but i need strpos to ignore matches of words such as:

 

Alice in Wonderland...

She uses wonder-bra...

 

I am thinking I could build a function to read the # of characters in the word, then run strpos() and check if there is whitespace around the word when found, if there is, return it, if not, ignore it. Though it seems to me there should be already a way to do this within strpos(), flags? or extra-values? Nothing in the manual talked about exact matches,  and preg_match_all is not a very frendly function for newbies... so basically i am asking if i should continue with strpos() or better start looking for something else?

Link to comment
https://forums.phpfreaks.com/topic/199050-making-strpos-return-exact-matches/
Share on other sites

After all, it seems preg_match is quite simple: i found this in the manual:

if (preg_match("/\b$wordLW\b/i", $v)) {
return $v;
}

 

 

$wordLW is the word i am looking for

$v is the text or sentence to search the word in

 

It runs a insensitive search for the word and only returns exact matches...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.