1

I'm wondering if it's possible to use PHP's parser to parse files and search/find a given function call.

For example, I want to know from which files the function eval is called. I could use find with -exec and some regex, but that returns a lot of false positives, and it also returns commented code.

My question is: can I use somehow PHP's own parser to search in files and say if a given function/reserved word is used in that file?

7
  • Why not just open it with fopen() and do a search for your keyword ? Commented Mar 20, 2014 at 12:08
  • Not with regex. There is infinite count of options to call some function named $function in PHP. call_user_func or $function() are most easiest from them Commented Mar 20, 2014 at 12:09
  • @ShankarDamodaran I'm not really sure I understand what you suggest. Can you elaborate a little bit further your idea? Commented Mar 20, 2014 at 12:09
  • 3
    Using Nikic's PHP parser would be a better approach Commented Mar 20, 2014 at 12:09
  • @AlmaDo Those are corner cases. I'm be more than happy if I could get at least the direct calls. Also, if I'm using PHP's parser it should be possible (somehow) to let it know that there is actually a call to what I'm looking for. Commented Mar 20, 2014 at 12:10

1 Answer 1

3

You can use PHP's internal tokenizer to find direct calls to eval:

<?php
$data = file_get_contents('test.php');
$tokens = token_get_all($data);

foreach($tokens as $token){
        if($token[0]==T_EVAL){
                echo "Eval found on line: ".$token[2]."\n";
        }
}

If you want to look for other things you can change the constant from T_EVAL to one of the constants specified here: Tokens

Sign up to request clarification or add additional context in comments.

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.