Jump to content

Scanning a page


lihman

Recommended Posts

I need to scan a page for certain words. If the page has those words, then i want to perform a certain action and if not, then a different action. However, I don't need all of the words to be present on the page, just one of them.

 

How would I do that?

 

For example, if the words that I wanted to look for are:

 

Green

Blue

Red

 

and $result contained:

 

The blue cat was big.

 

I want if(preg_match.....

 

to say yes even though red and green weren't present.

 

Would I use preg_match or preg_match_all or something else? I basically need like an 'OR' between the terms I am looking for in $result.

Link to comment
Share on other sites

The simplest solution would be to use alternation, which by definition basically means 'OR'.

 

if( preg_match( '#(?:Green|Blue|Red)#i', $input ) ) {
   // do action
} else {
   // do other action
}

Link to comment
Share on other sites

might have to benchmark it and it certainly depends on how big the subject is (and in the end of the day diff might be too small to care...) but in general if you are just wanting to check a string within a string (not looking to match a pattern) the built-in string search functions are faster.  strpos, stripos, strstr or stristr, etc...

Link to comment
Share on other sites

  • 3 weeks later...

The hashes are delimiters, all PCRE patterns must have delimiters (in much the same way a string must be delimited with " or '). The i is a modifier, more specifically the case insensitive modifier.

 

PCRE

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.