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
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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.