Created
March 3, 2021 19:33
-
-
Save davidkryzaniak/368c7d498733e025561112ada5026ef2 to your computer and use it in GitHub Desktop.
Get all variables used in a php file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Find all the `$variables` in a given PHP file | |
* @var string $path to a php file | |
* @return string[] list of variable (not including the "$") | |
*/ | |
function dk_get_vars_in_file($path) { | |
$php_code = file_get_contents($path); | |
$known_tokens = []; | |
foreach(token_get_all($php_code) as $token) { | |
$token = $token[1]; | |
if ('$' === substr($token,0,1)) { | |
$known_tokens[] = str_replace('$','',$token); | |
} | |
} | |
return $known_tokens; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment