Parse PHP from string

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
nbabenko
Forum Newbie
Posts: 2
Joined: Thu Nov 13, 2008 5:46 am

Parse PHP from string

Post by nbabenko »

Hi there.

Been searching around for good PHP help forums for a long time, let's hope this is the one that will bring me the most help.

Ok, well I'm developing a PHP template system, allowing the passing of variables and functions in its own unique way, similar to Smarty.

My issue at the moment, is preventing me from passing dynamic variables. Here is an example of a array call through the template system.

Code: Select all

[core.post.register_nickname]
Now, as the profile/register fields are also dynamically called from the database, the nickname part must be able to be called via another variable, within a loop.

Code: Select all

 
            $var_split = explode('.', $arg);
 
            foreach($var_split as $var)
            {
                if($count == 0)
                {
                    $echo = $this->vars[$var];
                }
                else
                {
                    if($echo[$var])
                    {
                        $echo = $echo[$var];
                    }
                    else
                    {
                        if($var)
                        {
                            $echo = $echo['\'' . preg_replace('/{(.*?)}/i', "$\\1", $var) . '\''];
                        }
                    }
                }
                                        
                $count++;
            }
 
So, all in all, it splits at the dots and loops through them, appending the current key to the $echo variable.

But, my issue lies here:

Code: Select all

$echo = $echo['\'' . preg_replace('/{(.*?)}/i', "$\\1", $var) . '\''];
The value of $var is register_{var} and I am trying to parse {var} to a normal variable to come from the key of a loop being processed.

If anyone could analyse the code and see if you could come up with a solution I'd be extremely grateful.

Thanks,
Nick.
swellular
Forum Newbie
Posts: 3
Joined: Thu Nov 13, 2008 9:05 am

Re: Parse PHP from string

Post by swellular »

This is not tested, but this might be what you want:

Code: Select all

$echo = $echo[''' . preg_replace('/{(.*?)}/i', "${\\1}", $var) . '''];
Or you may have to break it down into smaller chunks to get your required result.
Post Reply