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

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
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

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

Post 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
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

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

Post 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 :)
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post by jackpf »

You could escape the variables with a backslash.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

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

Post 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...
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

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

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post 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. :)
Post Reply