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

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...

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]));

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.