0

I am building a module for my own PHP framework, so my question is very specific and special. It's difficult to explain my question so I will go ahead and show it on code below.

I have a little piece of PHP in a $code variable, it looks like this:

$code = "___echo(TODAY_IS, date('j.n.Y', time()), time());"; 

What I need is to parse this $code variable and I want to get this result:

 $result = array(
   'function_name' => "___echo",
   'arguments' => array(
     0 => "TODAY_IS",
     1 => "date('j.n.Y', time())",
     2 => "time()"
   )
 );

I am thinking and I have tried using some regex, but neither worked sufficiently well. I also tried using Tokenizer, however I wasn't successful either.

Thanks for any hints or help in advance.

7
  • 3
    It will be exceedingly difficult to do this right all the time. Any framework, that resorts to writing its own interpreter, is a failed framework in my eyes. You should simplify the syntax of your $code string. Commented Sep 13, 2014 at 11:07
  • LOL, I can't say I wasn't expecting this comment :-) On my defense, the framework is doing some really advanced things and downside is I have to deal with such strange problems like this one. Commented Sep 13, 2014 at 11:22
  • Give an example of such an advanced thing and I will show a way without the need of a parser. Commented Sep 13, 2014 at 11:27
  • >>> the framework is doing some really advanced things This is not advanced. This is only BADvanced. Commented Sep 13, 2014 at 11:48
  • Just to give explanation where this piece of code comes from - it's module which precompiles PHP templates with i18n support so heavy stuff is dealt with only once and next requests are served with compiled templates. Commented Sep 13, 2014 at 11:58

2 Answers 2

2

Here is a shot using PHP-Parser. It's likely going to be more useful than tokenizer or some freaky regex.

Example:

$code = "___echo(TODAY_IS, date('j.n.Y', time()), time());";

$parser = new PhpParser\Parser(new PhpParser\Lexer);
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;

$statements = $parser->parse("<?php $code");

$result['function_name'] = $statements[0]->name->toString();
foreach ($statements[0]->args as $arg) {
    $result['arguments'][] = $prettyPrinter->prettyPrint(array($arg));
}

var_export($result);

Output:

array (
    'function_name' => '___echo',
    'arguments' =>
        array (
            0 => 'TODAY_IS',
            1 => 'date(\'j.n.Y\', time())',
            2 => 'time()',
        ),
)
Sign up to request clarification or add additional context in comments.

Comments

0

token_get_all() function is what you need here:

token_get_all("<?php ___echo(TODAY_IS, date('j.n.Y', time()), time());")

This returns a list of tokens parsed from the given string. See the tokens documentation for recognizing the items of the list.

In my opinion, tokenizer-based solution should be preferred over any regular expressions based on whatever is written in the PHP manual regarding syntax.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.