Jump to content

Regex to find Variables Names & Function Names inside PHP Source File


tserfer

Recommended Posts

Hello i am making a script that dumpes the function names and all variables names from a file. I dont want the get_defined_functions() file etc since i will not include it. I just get the source using file_get_contents.

 

So how can i find all function names and variables names from a PHP File?

 

Thank you

Link to comment
Share on other sites

Assuming the code is valid, scan for T_FUNCTION tokens and pull the first T_STRING or literal '(' following it. (Won't be immediately after.) If you grabbed a parenthesis then it was an anonymous function, as in

$anonymous = function() {};
[edit] If you need to account for namespaces then it's a bit more complicated... Edited by requinix
Link to comment
Share on other sites

OK, I was bored.  Just a simple example with regex:

preg_match_all('/function([^\(]+)\s*\(/i', $file, $functions);
print_r($functions[1]);

preg_match_all('/\$([a-z_][a-z0-9_]*)/i', $file, $variables);
print_r(array_unique($variables[1]));
Edited by AbraCadaver
Link to comment
Share on other sites

I too was very bored, so here is some code to parse token_get_all for them.

 

I threw in class names too.. those should arguably be included.

 

I also threw in constants. Now, this will only return php-defined constants (like `PHP_EOL`). Since the code isn't actually being evaluated, there's no way to check if it's a constant with defined(). Which means you'd have to kinda determine for yourself if it's a constant, based on the context. Which isn't as easy as it sounds. Especially if the code has syntax errors. I'm not going to outright say it's impossible but my attention span only goes so far when I'm doing something for free ;)

 

$namedTokens = array(
  'VARIABLE' => array(),
  'USER_FUNCTION' => array(),
  'CLASS' => array(),
  'PHP_FUNCTION' => array(),
  'PHP_CONSTANT' => array()
);	
foreach ($tokens as $key => $token) {
  if (is_array($token) && isset($token[0]) && is_int($token[0])) {
    $tn = token_name($token[0]);
    switch ($tn) { 
      case 'T_VARIABLE' : 
        if (!in_array($token[1], $namedTokens['VARIABLE'])) 
          $namedTokens['VARIABLE'][] = $token[1];
        break;
      case 'T_FUNCTION' :
      case 'T_CLASS' :
        if (isset($tokens[$key+1])   &&
            is_array($tokens[$key+1])&&
            isset($tokens[$key+1][0])&&
            token_name($tokens[$key+1][0])=='T_WHITESPACE')
        if (isset($tokens[$key+2])   &&
            is_array($tokens[$key+2])&&
            isset($tokens[$key+2][0])&&
            token_name($tokens[$key+2][0])=='T_STRING') {
              switch ($tn) {
                case 'T_FUNCTION' : $nt = 'USER_FUNCTION'; break;
                case 'T_CLASS'    : $nt = 'CLASS'; break;								
              }
              $namedTokens[$nt][] = $tokens[$key+2][1];
        }
        break;
      case 'T_STRING' :
        /* php native function */
        if (function_exists($token[1])&&!in_array($token[1], $namedTokens['PHP_FUNCTION']))
          $namedTokens['PHP_FUNCTION'][] = $token[1];
        /* php native constants*/ 
        if (defined($token[1])&&!in_array($token[1], $namedTokens['PHP_CONSTANT']))
          $namedTokens['PHP_CONSTANT'][] = $token[1];
        break;
    } // end switch
  } // end if
} // end foreach


// example:
print_r($namedTokens);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.