lihman Posted May 15, 2010 Share Posted May 15, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/201817-scanning-a-page/ Share on other sites More sharing options...
cags Posted May 15, 2010 Share Posted May 15, 2010 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 } Quote Link to comment https://forums.phpfreaks.com/topic/201817-scanning-a-page/#findComment-1058653 Share on other sites More sharing options...
.josh Posted May 18, 2010 Share Posted May 18, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/201817-scanning-a-page/#findComment-1059846 Share on other sites More sharing options...
GoneNowBye Posted June 7, 2010 Share Posted June 7, 2010 #(?:Green|Blue|Red)#i was given above what does the # do and is the i at the end a qualifier of any description? Quote Link to comment https://forums.phpfreaks.com/topic/201817-scanning-a-page/#findComment-1069124 Share on other sites More sharing options...
cags Posted June 7, 2010 Share Posted June 7, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/201817-scanning-a-page/#findComment-1069126 Share on other sites More sharing options...
GoneNowBye Posted June 7, 2010 Share Posted June 7, 2010 thanks Quote Link to comment https://forums.phpfreaks.com/topic/201817-scanning-a-page/#findComment-1069140 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.