andytan91 Posted August 10, 2010 Share Posted August 10, 2010 Hello guys i have been trying to find an alternative file function to read multiple files into an array. My aim is to preg_replace every file in directory starting with Audit_Report with the font colours...i have been googling it for like 2 hours and the closest answer i can get to is using the glob() function but it doesn't work for me still. I would like you guys to give me suggestions if you can..thanks!! <?php $file = "Audit_Report.(SEAN).(192.168.199.129).txt"; $lines = file($file); foreach ($lines as $line) { $string = $line; $patterns = array(); $patterns[0] = '/\bPass\b/'; $patterns[1] = '/\bFail\b/'; $patterns[2]='/\b============ Major Audit and Account Policies============ \b/'; $replacements = array(); $replacements[1] = "<font color=red>Fail</font>"; $replacements[0] = "<font color=green>Pass</font>"; $replacements[2] = "<br><b>==== Major Audit and Account Policies====</b>"; ksort($patterns); ksort($replacements); $a = preg_replace($patterns, $replacements, $string); echo "$a<br />\n"; file_put_contents(basename($file, substr($file, strpos($file, '.'))) . ".html","$a<br />\n", FILE_APPEND) or die("Cannot write file"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/210302-alternative-file-function-for-wildcard-extension/ Share on other sites More sharing options...
JasonLewis Posted August 10, 2010 Share Posted August 10, 2010 You want to put every file in a directory into an array? Or put every files contents into an array? I'm confused... Start with something like this: foreach(glob("*.txt") as $file){ if(preg_match("#^Audit_Report#", basename($file))){ echo $file . ' is an audit report!<br />'; // you could use file_get_contents to get the contents of the file and change it or whatever you wanted to do. } } That will search through all the files in your current directory and check if they start with Audit_Report, it's case sensitive too. Use the i modifier to make it case insensitive. Quote Link to comment https://forums.phpfreaks.com/topic/210302-alternative-file-function-for-wildcard-extension/#findComment-1097420 Share on other sites More sharing options...
salathe Posted August 10, 2010 Share Posted August 10, 2010 For what it's worth, in reply to ProjectFear, you could move the job of checking the file name into the glob itself... glob("Audit_Report*.txt") Quote Link to comment https://forums.phpfreaks.com/topic/210302-alternative-file-function-for-wildcard-extension/#findComment-1097434 Share on other sites More sharing options...
JasonLewis Posted August 10, 2010 Share Posted August 10, 2010 For what it's worth, in reply to ProjectFear, you could move the job of checking the file name into the glob itself... glob("Audit_Report*.txt") And now I know.. Quote Link to comment https://forums.phpfreaks.com/topic/210302-alternative-file-function-for-wildcard-extension/#findComment-1097437 Share on other sites More sharing options...
andytan91 Posted August 10, 2010 Author Share Posted August 10, 2010 I tried the glob function, it only show the files which exist in the directory...Hmm what i want is to read and replace every files in the directory starting with the name Audit_Report with the codes under the Foreach statement..hence i will need to find a way for the file() function to accept wildcard extensions.. Quote Link to comment https://forums.phpfreaks.com/topic/210302-alternative-file-function-for-wildcard-extension/#findComment-1097622 Share on other sites More sharing options...
JasonLewis Posted August 11, 2010 Share Posted August 11, 2010 So you want to edit each file? Well, start with that loop to read all the files. Then in the loop open each file and edit them. Quote Link to comment https://forums.phpfreaks.com/topic/210302-alternative-file-function-for-wildcard-extension/#findComment-1097867 Share on other sites More sharing options...
andytan91 Posted August 11, 2010 Author Share Posted August 11, 2010 Thanks dude for the help...now the script manages to read every file that i want! However, i am stuck at the file put contents part..lets say for example i have 2 files named Audit_Report_Andy and Audit_Report_Sean. I will need it to output to its individual html file. As of now the problem is the file_put_contents outputs the result of a two files to a single combined html file .. <html> <head> </head> <body> <?php //$file = "Audit_Report.(CLIENT).( 192.168.199.134).(2010-08-1023h59m54s).txt"; //$lines = file($file); foreach(glob("Audit_Report*.txt") as $file){ $lines = file($file); foreach($lines as $line) { $string = $line; $patterns = array(); $patterns[0] = '/\bPass\b/'; $patterns[1] = '/\bFail\b/'; $patterns[2] = '/:/'; $patterns[3] = '/\btable\b/'; $patterns[4] = '/\bendtable\b/'; $patterns[5] = '/\bServicePackSetting\b/'; $patterns[6] = '/\bMajorAuditandAccountPolicies\b/'; $patterns[7] = '/\bAuditPolicy\b/'; $patterns[8] = '/\bAccountPolicy\b/'; $replacements = array(); $replacements[1] = "<font color=red>Fail</font></tr></td>"; $replacements[0] = "<font color=green>Pass</font></tr></td>"; $replacements[2] = ":</td><td>"; $replacements[3] = "<table border=1 cellpadding=1 cellspacing=0 class=whitelinks>"; $replacements[4] = "</table>"; $replacements[5] = "<b>Service Pack</b></td><td></tr></td>"; $replacements[6] = "<b>Major Audit and Account Policies</b></td><td></tr></td>"; $replacements[7] = "<b>Audit Policy</b><br></td><td></tr></td>"; $replacements[8] = "<b>Account Policy</b><br></td><td></tr></td>"; ksort($patterns); ksort($replacements); $a = preg_replace($patterns, $replacements, $string); //echo "$a<br />\n"; file_put_contents(basename($file, substr($file, strpos($file, '.'))) . ".html","<tr><td>$a",FILE_APPEND) or die("Cannot write file"); } } Quote Link to comment https://forums.phpfreaks.com/topic/210302-alternative-file-function-for-wildcard-extension/#findComment-1097949 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.