tags:

views:

62

answers:

1

I have built variable $line based on the following:

foreach ($regex as $each){
 $parts = explode('::',$each);
 $pattern = '"/^'.$parts[1].'/i"';
 $subject = '$row['.$parts[0].']';

 $line .= 'preg_match'.'('.$pattern.','.$subject.')';
 if (end($regex) != $each){
 $line .= '&&';
 }
}

I have a function that call the $line. Once called, echo $line produces the following output:

preg_match("/^ab/i",$row[RG])&&preg_match("/^cd/i",$row[EX])

I am trying to use the $line variable in one of the if loops and preg_match doesn't work. However, if I were to copy and paste the value of $line in the if statement it works just fine. Any input would be appreciated!

+3  A: 

Here is an alternative solution to premisos and probably better:

$passed = true;
foreach ($regex as $each){
 $parts = explode('::',$each);
 $pattern = '/^'.$parts[1].'/i';
 $subject =  $row['"'.$parts[0].'"'];

 if (!preg_match($pattern, $subject)) {
     $passed = false;
     break;
 }
}

if ($passed) {
    echo 'Woohoo! It passed!';
}

That would probably be the prefered method, because if one preg_match fails, it all fails and there is no need to continue.

MisterPhilip
Ah yes, this is much better and more secure. Nice one.
Brad F Jacobs