Skip to content

Instantly share code, notes, and snippets.

@davidkryzaniak
Created March 3, 2021 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidkryzaniak/368c7d498733e025561112ada5026ef2 to your computer and use it in GitHub Desktop.
Save davidkryzaniak/368c7d498733e025561112ada5026ef2 to your computer and use it in GitHub Desktop.
Get all variables used in a php file
<?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