Jump to content

Get a list of unique variables from a script


mrherman

Recommended Posts

I have a long PHP script that I didn't write.  I'd like to get a simple text list of all the unique variables from the script -- all variables within the script that begin with $, such as

 

$_ADMIN

$query

$category

$user

$id

$result, etc.....

 

Any ideas how a PHP dabbler can go about this?  I did a PHPFreaks search, but didn't find a thread on this.

 

Thank you!

this will search and print out for any code starting with $ and ending with a space. it's not perfect though, cause if you have something like this $var="whatever"; there is no space after the variable. But it's a start. All your variables should show up on the list, except some will have other stuff after them.

replace index.php for your file's name and place them in same directory.

<?php
$file = file_get_contents('index.php');
//echo $file;
preg_match_all('/\$(.*?)\\s/is', $file, $matches);
echo implode("<br>",$matches[0]);
?>

 

Hope it helps

 

EDIT: you can also apply array_unique to filter out duplicates.

Get an editor that will do regular expression matching, and search for this:

 

Well, I would counter that a good editor will automatically display all the variables used in a script.

 

True, but I didn't say a good editor, I just said one that would support regular expressions.

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.