I need to check if any of the lines "hello", "i am", "dumb" exist in a longer line called $ ohreally , if even one of them exists, my test is completed and I have knowledge about that none of them will appear if one of them has.
In these conditions, I ask you for help on the most efficient way to record this search,
strpos () 3 times is how is it?
if (strpos ($ohreally, 'hello')){return false;} else if (strpos ($ohreally, 'i am')){return false;} else if (strpos ($ohreally, 'dumb')){return false;} else {return true;}
or one preg_match?
if (preg_match('hello'||'i am'||'dumb', $ohreally)) {return false} else {return true};
I know that the preg_match code is incorrect, I would really appreciate it if someone could suggest the correct version.
Thanks!
Answer
Please read what cletus said and the middaparka test roared. I also ran a mirco time test on different lines, long and short. with these results
IF, you know the likelihood that string values will have an ORDER value from most probable to least. (I did not notice presentable differences in the ordering of the regular expression itself, that is, between /hello|i am|dumb/ or /i am|dumb|hello/ .
In consecutive strpos , on the other hand, probability matters. For example, if "hello" is 90%, "I" is 7% and "dumb" is 3 percent of the time. would you like to organize your code to check hello first and exit the function as soon as possible.
my microtime tests show this.
for haystacks, A, B, and C, in which the needle is on the first, second, and third strpos (), respectively, the time is as follows:
StrPos:
A: 0.00450 seconds // 1 strpos ()
B: 0.00911 seconds // 2 strpos ()
C: 0.00833 seconds // 3 strpos ()
C: 0.01180 seconds // 4 strpos () one extra added
and for preg_match:
A: 0.01919 seconds // 1 preg_match ()
B: 0.02252 seconds // 1 preg_match ()
C: 0.01060 seconds // 1 preg_match ()
as the numbers show, strpos is faster until the 4th execution, so I will use it instead, since I only have 3 sub-stings to check :)