tserfer Posted November 4, 2013 Share Posted November 4, 2013 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 https://forums.phpfreaks.com/topic/283596-regex-to-find-variables-names-function-names-inside-php-source-file/ Share on other sites More sharing options...
AbraCadaver Posted November 4, 2013 Share Posted November 4, 2013 Might check reflection class (but I think they will need to be loaded), or: print_r( token_get_all ( file_get_contents( $filename ) ) ); Link to comment https://forums.phpfreaks.com/topic/283596-regex-to-find-variables-names-function-names-inside-php-source-file/#findComment-1456942 Share on other sites More sharing options...
requinix Posted November 4, 2013 Share Posted November 4, 2013 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... Link to comment https://forums.phpfreaks.com/topic/283596-regex-to-find-variables-names-function-names-inside-php-source-file/#findComment-1456947 Share on other sites More sharing options...
AbraCadaver Posted November 4, 2013 Share Posted November 4, 2013 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])); Link to comment https://forums.phpfreaks.com/topic/283596-regex-to-find-variables-names-function-names-inside-php-source-file/#findComment-1456949 Share on other sites More sharing options...
tserfer Posted November 4, 2013 Author Share Posted November 4, 2013 preg_match_all('/function([^\(]+)\s*\(/i', $file, $functions); This works in most cases but actually i want all the defined functions including PHP standard functions. Is that possible? Link to comment https://forums.phpfreaks.com/topic/283596-regex-to-find-variables-names-function-names-inside-php-source-file/#findComment-1456956 Share on other sites More sharing options...
.josh Posted November 5, 2013 Share Posted November 5, 2013 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 https://forums.phpfreaks.com/topic/283596-regex-to-find-variables-names-function-names-inside-php-source-file/#findComment-1456960 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.