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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.