tags:

views:

62

answers:

4

I'm converting one of my old Perl programs to PHP, but having trouble with PHP's string handling in comparison to Perl.

In Perl, if I wanted to find out if $string contained this, that or the_other I could use:

if ($string =~ /this|that|the_other/){do something here}

Is there an equivalent in PHP?

+1  A: 

You can use PHP's preg_match function for a simple regex test.

if ( preg_match( "/this|that|the_other/", $string ) ) { 
    ...
}
friedo
+1  A: 

In PHP you can use preg_match function as:

if( preg_match('/this|that|the_other/',$string) ) {
  // do something here.
}
codaddict
+1  A: 

You can either use a regular expression (e.g. preg_match):

if(preg_match('/this|that|the_other/', $string))

or make it explicit (e.g. strstr):

if(strstr($string, 'this') || strstr($string, 'that') || strstr($string, 'the_other'))
Felix Kling
A: 

THanks all, much easier than I feared!

Grant
@Grant: The stackoverflow way of saying thanks is to accept the most helpful answer :)
codaddict