Sometimes you need to find the variables from a php file. I needed it to reverse engineer a script. I was checking which variables and objects are passed to the script. So I wrote my own parser for it. It’ll find all the variables from the document. It’ll only work for php.
Here is the code.
<?php
$con = file_get_contents("php://stdin");
$reg = '[a-zA-Z_][a-zA-Z_0-9]*';
$reg_v = "\\\${$reg}";
$reg_s = "$reg_v(?!\-\>)";
preg_match_all("/$reg_s/i",$con,$m);
$reg_s = "$reg_v\->$reg";
preg_match_all("/$reg_s/i",$con,$m1);
//$reg_a = '\[([\'"])([^\'"]+)\1\]';
//preg_match_all("/$reg_v$reg_a/i",$con,$m2);
//preg_match_all("/$reg_s$reg_a/i",$con,$m3);
foreach(array_unique(array_merge($m[0],$m1[0],$m2[0],$m3[0])) as $v):
echo $v.PHP_EOL;
endforeach;
?>
Save this as a file like vars.php. Then issue the following code for your subject file (subject.php).
# php /path/to/vars.php < /path/to/subject.php
$desc
$key
$value
$col
$__tags
$hex
$rand_size
$tags_n
$tpl->showPlayer
$tpl->playerHeight
$tpl->playerWidth
$tpl->viedoId
That was the output for me.
Hope it helps.

{ 2 } Comments
Why do you need $reg_v and $reg_s? Could you clarify it?
You may prettify your code using google-prettify. See here
http://google-code-prettify.googlecode.com/svn/trunk/README.html
$reg_v and $reg_s?
So that the regular expression doesn’t get way too big.
You’ll also see how did I reuse those variables later.
Again if you un-comment the 3 lines it will work for array.
Thanks for the google-prettify.
Post a Comment