Page 1 of 1

Not to use eval()

Posted: Sun Nov 23, 2003 3:14 pm
by ChibiGuy
If I have a string lwith the following value:

Code: Select all

( preg_match('/^(.+)?ab(.+)?$/iU', $value[0]) )
Is the only way I can see if that statement evaluates to TRUE is through an eval() statement? The problem is that I can be running this statement through a foreach loop that can run for as long as 16000 iterations, and eval() seems to slow the execution time down tremendously. I need a workaround.

Thanks for any help

Posted: Sun Nov 23, 2003 3:24 pm
by bionicdonkey
taken from the php manual:

Code: Select all

<?php 
// The "i" after the pattern delimiter indicates a case-insensitive search 
if (preg_match ("/php/i", "PHP is the web scripting language of choice.")) { 
   print "A match was found."; 
} else { 
   print "A match was not found."; 
} 
?>

Posted: Sun Nov 23, 2003 3:52 pm
by ChibiGuy
yea i understand that. But what if the preg_match() statement is inside of a string? How can i check to see whether it will evaluate to true, without using eval().

Posted: Sun Nov 23, 2003 10:25 pm
by Gen-ik
ChibiGuy wrote:yea i understand that. But what if the preg_match() statement is inside of a string? How can i check to see whether it will evaluate to true, without using eval().
Why is your preg_match() statement inside of a string?

Posted: Mon Nov 24, 2003 2:42 pm
by ChibiGuy
becaause the function takes an array like array('id =~ %ab[^B]%', 'AND', 'name <> name') and tries to convert it to php code to test it against some information.

Posted: Mon Nov 24, 2003 3:38 pm
by Gen-ik
Not sure if this will help but you will still need to eval() the string in order for any PHP inside of it to be treated like PHP.

Code: Select all

<?php

$theString = "$"."aResult = ( preg_match('/^(.+)?ab(.+)?"."$"."/iU', "."$"."value[0]) );"
eval($theString);

if($aResult)
{
      echo "preg_match is true";
}
else
{
      echo "preg_match is false";
}

?>

Posted: Mon Nov 24, 2003 3:47 pm
by ChibiGuy
from my first post.
Is the only way I can see if that statement evaluates to TRUE is through an eval() statement? The problem is that I can be running this statement through a foreach loop that can run for as long as 16000 iterations, and eval() seems to slow the execution time down tremendously. I need a workaround
I need a workaround for eval().

Posted: Mon Nov 24, 2003 3:57 pm
by Gen-ik
Personally I can't think of a work-around for it apart from rethinking the way your code works.

eval() evaluates a $string a PHP... that's what it's designed for and I think it's the only way you are going to be able to do what you are doing.

Posted: Mon Nov 24, 2003 6:42 pm
by ChibiGuy
yea that's what I was thinking. There is a work around for the logic, but a great sacrifice for execution time though. I guess eval() will have to do.

Thanks,