0

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
  • Can you format the code as code? It would make it much easier to read. Commented Jul 27, 2010 at 4:33
  • Also, what do you mean you have a function that call "the $line"? $line appears to be a string, not a function. Commented Jul 27, 2010 at 4:34
  • Hi! I meant that I pass $line to a function that calls mssql stored proc returning values for $row[RG] & $row[EX] later on in the script. The issue is that preg_match doesn't work if the $line is used in one of the if statements, however, if I were to copy and paste the value of $line (i.e. preg_match("/^ab/i",$row[RG])&&preg_match("/^cd/i",$row[EX]) ) into the same if statement it works just fine. Thank you very much for your help! Commented Jul 27, 2010 at 4:44

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah yes, this is much better and more secure. Nice one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.