PDA

View Full Version : Trouble using eval()


thincom2000
01-07-2007, 11:37 PM
I am trying to write a script that uses eval() to parse vBulletin variables and phrases from a string. However, I get a parse error if the string contains a colon : and probably if I tried a semicolon ; as well.

Is there a way to escape the colons with a function such as addslashes()?

How do I get around this?

nico_swd
01-08-2007, 11:06 AM
They don't need to be escaped. Can you post your code?

thincom2000
01-15-2007, 03:02 AM
Okay I got around escaping the colons. But here's what I'm trying to do:

The user enters a string containing a list of variables in an input field. These variables then need to be interpreted as globals, but the following oh so simple code won't work:

global eval('return $user_entered_globals;');

:confused:

EDIT: Never mind. Although this would be nice to know for the future, I decided against user-entered globals this time around.

harmor19
01-15-2007, 06:26 AM
Wouldn't you use

<?php

$user_entered_globals = array("test1", "test2", "test3");

if(!in_array($_GET['test'], $user_entered_globals))
{
die("Error!");
}

function something()
{

global $user_entered_globals;

eval('$user_entered_globals');

}

?>

This code is untested

thincom2000
01-16-2007, 02:36 AM
Got it working.

$test1 = 'string';
$test2 = Array('string1', 'string2');
$test3 = some_class;

$user_entered_globals = '$test1, $test2, $test3';

eval('global ' . $user_entered_globals . ';');

This is very simplified, but working. :rolleyes: