<?php
        error_reporting
(0);
        
file_put_contents("output.txt","");
        
// These are what we are looking for. first we list the index of the info we want, than the pattern
        
$patterns = array(
                        
'brand'         => array('2'=>'/(System Manufacturer:)\s+(.+)/'),
                        
'model'         => array('2'=>'/(System Model:)\s+(.+)/'),
                        
'mac'           => array('3'=>'/\s+(Physical Address)(\.\s)+:\s(.+)/'),
                        
'OS'            => array('2'=>'/(OS Name:)\s+(.+)/'),
                        
'OSVer'         => array('2'=>'/(\nOS Version:)\s+(.+)/'),
                        
'RAM'           => array('2'=>'/(Total Physical Memory:)\s+(.+)/'),
                        
'HDD'           => array('1'=>'/\s+(([0-9]{1,3}\.[0-9]{1,3}\s|[0-9]{1,3}\s)(GB|MB))\sTotal.+/'),
                        
'processor'     => array('1'=>'/\s+.+\~([0-9]+\sMhz)/')
                );
        
// the results of our regexs are stored here
        
$data = array();
        
        
// the output
        
$output "";

        
// get all files to open
        
$files glob("inv/*");
// int/* was coloring everything, so i put this here to get rid of it! */
        // for each file, store its contents
        
foreach($files as $file) {
                
$content[$file] = file_get_contents($file);
        }
        
// for each content stored (each file), store its name and lines and key=>val pairs
        
foreach($content as $name=>$lines) {
                
// clean the name
                
$name trim(str_replace(array("inv/",".txt"),"",$name."\n"));
                
// for each of our patterns, store the category (ram, os, etc) and info (index and regex pattern) as a key=>val pair
                
foreach($patterns as $category=>$info) {
                        
// for each of our index, regex arrays, store the index and pattern as key=>val pairs
                        
foreach($info as $index=>$pattern) {
                                
// check all of our content for the given pattern
                                
preg_match_all($pattern,$lines,$matches);
                                
// if we have a match, store as data > computer name > category => the index of the match we're looking for
                                
if ($matches) { $data[$name][$category] = $matches[$index]; }
                        }
                }
                
// put the file into an array instead of just a lump of lines
                
$lines_arr explode("\n",$lines);
                
// the user name is the first line
                
$data[$name]['user'][] = $lines_arr[0];
                
// location is the second
                
$data[$name]['location'][] = $lines_arr[1];
                
// type is the third
                
$data[$name]['type'][] = $lines_arr[2];
        }
        
// for each piece of data (our computers), store the name of the computer, and the data for that computer as key=>val pairs
        
foreach($data as $name=>$computer) {
                
// start our table with the computer name
                
$output .= "| $name | ";
                
// for each piece of data on a PC, store the category (hdd, ram, etc) and info (arrays that contain "how much ram" or how much hdd space, etc) as key=>val pairs
                
foreach($computer as $category=>$info) {
                        
// if we have more than 1 result for a given category (like 2 processors), don't echo a pipe, just a space
                        
if (count($info) > 1) {
                                foreach(
$info as $details) {
                                        if (
$category == "mac") {
                                            
$details str_replace("-",":",$details);
                                        }
                                        
$output .=  trim($details)." ";
                                }
                                
// echo a pipe to finish off the multi-result category
                                
$output .=  "| ";
                        
// if we have no results, we still need to echo a pipe to have an empty table (no harddrives magically?)
                        
} else if(count($info) == 0) {
                                        
$output .=  " | ";
                        
// when we have 1 result for a given category...
                        
} else {
                                
// for each of our info arrays, extract info we need
                                
foreach($info as $details) {
                                        if (
$category == "mac") {
                                            
$details str_replace("-",":",$details);
                                        }
                                        
// echo it
                                        
$output .=  trim($details)." | ";
                                }
                        }
                }
                
// start a new line for the next box, and cause it's prettier
                
$output trim($output)."\r\n";
                if (!
file_put_contents("output.txt",$output,FILE_APPEND)) {
                    echo 
$output;
                }
                
$output "";
        }
?>