Page 1 of 1

create_function() parse error -- can you spot the error?

Posted: Sat Jul 25, 2009 1:14 pm
by alex.barylski

Code: Select all

   $path = explode('/', $path);
    $path = array_map(create_function("$e", "return str_replace('articles', '', $e);"), array_filter($path));
    $path = implode('/', $path);
I'm getting the error:
Parse error: syntax error, unexpected ')' in /var/www/sites/demosite.com/system/core/functions.php(22) : runtime-created function on line 1
If I just return $e everything works as expected.

WTF is going on?

Cheers,
Alex

Re: create_function() parse error -- can you spot the error?

Posted: Sat Jul 25, 2009 1:24 pm
by alex.barylski
I changed the double quotes to single quotes in the create_function() and that seems to have solved the problem...bug or am I missing something about the use of this function? Double quoted strings are possibly disallowed due to variable interpolation??? Actually that makes sense otherwise how would the function know how to distinguish interpolated variables or function variables.

Nevermind, ignore me :)

Re: create_function() parse error -- can you spot the error?

Posted: Sat Jul 25, 2009 1:38 pm
by jackpf
You could escape the variables with a backslash.

Re: create_function() parse error -- can you spot the error?

Posted: Sat Jul 25, 2009 3:06 pm
by alex.barylski
You could escape the variables with a backslash.
I think I tried that with addslashes -- didn't do the trick tho. What did you have mind? In anycase the problem is solved but just for the sake of curiosity...

Re: create_function() parse error -- can you spot the error?

Posted: Sat Jul 25, 2009 3:48 pm
by Weirdan
PCSpectra wrote:bug or am I missing something about the use of this function?
You're missing the fact that variables in double quotes were interpolated before the string was passed to create_function(). Thus it was receiving "return str_replace('articles', '', );" - it's obviously invalid php syntax. When you changed double quotes to single, the variables stopped being interpolated, and create_function() received 'return str_replace('articles', '', $e); - which was valid.

Re: create_function() parse error -- can you spot the error?

Posted: Sat Jul 25, 2009 3:50 pm
by jackpf

Code: Select all

 
echo preg_replace_callback('/(.*?)/', create_function("\$matches", "return strtoupper(\$matches[0]);"), 'hello world');
 
Like that. Addslashes isn't supposed to escape variables. Anyways, they'd already be expanded by the time any function would be able to run on it, so that would be impossible. This is what addslashes does. :)