Forum Moderators: coopster

Message Too Old, No Replies

Finding multiple patterns in a string

         

benj0323

9:21 am on Oct 24, 2004 (gmt 0)

10+ Year Member



I know eregi allows you to find a pattern in a string, but is there a function that allows you to find multiple patters in a string. If its finds any of the patters then is true.

For example, here is how the regular eregi function is used:

eregi(needle,haystack)

I want to do this

eregi(needle, needle, needle, needle, haystack)

if it finds any of the needles then its true.

Thanks for your help.

mincklerstraat

9:28 am on Oct 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't know of a function like this, would suggest something along these lines:

$needlearray = array('needle1', 'needle2', 'needle3');
$findflag = 0;
foreach($needlearray as $v){
if(eregi($v, $haystack)){
$findflag = 1;
break;
} // end if
} // end foreach
if($findflag) echo 'found it!';

Hanu

9:31 am on Oct 24, 2004 (gmt 0)

10+ Year Member



eregi( "$needle1¦$needle2¦$needle3¦...", haystack );

Hanu

9:35 am on Oct 24, 2004 (gmt 0)

10+ Year Member



or even better (faster)

preg_match( "/$needle1¦$needle2¦$needle3/i", $haystack )

mincklerstraat

10:00 am on Oct 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sounds much better than my suggestion.

Hanu

6:18 pm on Oct 24, 2004 (gmt 0)

10+ Year Member



... if the number of needles is known and static. If it is dynamic, e.g. when an array of needles is given, your solution is better. May the poster decide what's better in his/her situation.

benj0323

3:54 am on Oct 25, 2004 (gmt 0)

10+ Year Member



Thanks for your help. I finally got it figured out.