Jump to content

Alternative File function for wildcard extension


andytan91

Recommended Posts

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");	

}


?>

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.

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

 

 

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");	
   }
      
      
   
}

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.