<?php
// Script by David B. Nagle
// Version 0.1
// http://randomfrequency.net/misc-code/scrape-php-globals/

function find_globals($filename$cols 79) {

    
// Get filecontents
    
$lines file($filename);

    
// Convert file to a single string
    
$file implode(''$lines);
    unset(
$lines);

    
// Remove /* ... */
    
$file preg_replace('/\057\052.*?\052\057/s','',$file);
    
// Remove // ...
    
$file preg_replace('/\/\/.*?$/m','',$file);

    
// Find all global statements
    
preg_match_all("/global .*?;/s"$file$globalsPREG_PATTERN_ORDER);

    
$vars = array();
    
    foreach(
$globals[0] as $g) {
        
// Get rid of the non-variable parts
        
$g preg_replace('/^global /s','',$g);
        
$g preg_replace('/;$/s','',$g);
        
// Convert all series of space characters to single spaces
        
$g preg_replace('/\s+/',' ',$g);
        
// Extract all variables
        
$temp explode(', '$g);
        
$vars array_merge($temp$vars);
    }

    
// Figure out the minimum length required for output line
    
$minlen strlen("global");

    
// Populate a hash with all the variable names
    
foreach ($vars as $v) {
        
$hash[$v] = 1;
        
// Add 1 for the comma/semicolon
        
if(strlen($v)+$minlen$minlen strlen($v)+1;
    }

    
// Coerce cols up to minlen if necessary
    
if($cols $minlen && $cols >= 0$cols $minlen;

    
// Sort array hash
    
ksort($hash);

    
// Create a new global command with all the variables
    
$disp implode(', 'array_keys($hash));
    
$disp "global $disp;";

    if(
$cols >= 0) {
        
// Generate the command, restrained by column width
        
$comma $cols-1;
        while(
$disp) {
            
preg_match("/^.{1,$cols}$|^.{1,$comma},/"$disp$matches);
            
$line $matches[0];
            
$disp substr_replace($disp''0strlen($line));
            
$disp preg_replace('/^ /','',$disp);
            
$output .= "$line\n";
        }
    } else {
        
$output "$disp\n";
    }

    return 
$output;
}
?>