I need to check for multi occurances in a string. At the moment I am checking for a single occurance using strpos() e.g. $pos = strpos($notice, "abc"); if ($pos !== false) { Dissallow notice } I now want to look for "abc" and "def" and "ghi" and then dissallow notice. Can anybody help me with the code for this. Or do I need to use the same code again for each occurance to look for. Thanks
You could use an else if clause, or you could do three variables with a an 'or' clause, or you could repeat the code three times. 1----------------- if(strpos($notice, "abc") !== false){ Dissallow notice } elseif(strpos($notice, "def") !== false){ Dissallow notice } elseif(strpos($notice, "ghi") !== false){ Dissallow notice }else { Allow notice } 2---------------------- $pos1 = strpos($notice, "abc"); $pos2 = strpos($notice, "def"); $pos3 = strpos($notice, "ghi"); if ($pos1 !== false) or ($pos2 !== false) or ($pos3 !== false) { Dissallow notice } 3---------------- $pos = strpos($notice, "abc"); if ($pos !== false) { Dissallow notice } $pos = strpos($notice, "def"); if ($pos !== false) { Dissallow notice } $pos = strpos($notice, "ghi"); if ($pos !== false) { Dissallow notice }
preg_match_all returns the number of occurences found in a string, I'd make a simple function using that instead of all that messing around ....
Many thanks to the people who replied. I used DRUIDELDER's suggestion. The only problem that I found was that I had to slightly modify this line: if ($pos1 !== false) or ($pos2 !== false) or ($pos3 !== false) to: if (($pos1 !== false) or ($pos2 !== false) or ($pos3 !== false)) Thanks also to krakjoe for his suggestion. I couldn't use his suggestion because I am a novice and I didn't understand regex or preg_match_all Being a novice I needed the example that DRUIDELDER gave me, but I am very appreciative for all suggestions. Does anybody know how to do the same thing in PERL ???