Not to use eval()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ChibiGuy
Forum Newbie
Posts: 22
Joined: Fri Jul 11, 2003 11:23 pm
Location: USA
Contact:

Not to use eval()

Post 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
bionicdonkey
Forum Contributor
Posts: 132
Joined: Fri Jan 31, 2003 2:28 am
Location: Sydney, Australia
Contact:

Post 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."; 
} 
?>
ChibiGuy
Forum Newbie
Posts: 22
Joined: Fri Jul 11, 2003 11:23 pm
Location: USA
Contact:

Post 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().
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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?
ChibiGuy
Forum Newbie
Posts: 22
Joined: Fri Jul 11, 2003 11:23 pm
Location: USA
Contact:

Post 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.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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";
}

?>
ChibiGuy
Forum Newbie
Posts: 22
Joined: Fri Jul 11, 2003 11:23 pm
Location: USA
Contact:

Post 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().
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
ChibiGuy
Forum Newbie
Posts: 22
Joined: Fri Jul 11, 2003 11:23 pm
Location: USA
Contact:

Post 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,
Post Reply