# PHP- How to grep thru a text file but ignore certain lines?



## mj0769 (Dec 25, 2007)

Hi all, hoping someone could help with a couple of php tips. I used to do some shell scripting years ago and remember being able to serch through files and gather vars using grep. 

I'm building a php-mysql app to manage a room full of boxes. Each box will have it's own data file. It's not too complicated but the app must be able to list boxes according to thier row and placement in the room and then give some stats on the overall number of boxes as well. I'm using a simple text file to manage the ranges of each row of boxes..i.e. 0001-0010; 0011-0020, etc. and so far I have it working ok.. I'm using the number of rows in the text file as a counter for a while-loop. Where I'm getting stuck is how to tell php to ignore certain lines in the text file when it reads through.i.e. comment lines. If I remember right, grep used a -i flag to ignore unwanted lines.. you could pipe the grep command into a while loop from there. I can't seem to find the equivalent thing in php.. 

Thanks for any help.. 
-M


----------



## mj0769 (Dec 25, 2007)

btw, here's the gist of the code:

EXCERPT FROM TEXT FILE: boxRanges.txt
1982-1990;
1991-1999;
2000-2008;
2009-2017;
2018-2026;
2027-2035
======================

//PHP FILE 
$myFile = "boxRanges.txt";
$fh = fopen($myFile, 'r')or die("can't open file");
$fs = filesize($myFile);
$Lines = file($myFile);	
$LineCount = count($Lines);
$Data = array();
$i = 0;

// READS IN FILE, PARSE BY ";" LOADS INTO ARRAY
$theData = fread($fh, $fs);
fclose($fh);
$thisArray = (explode(";", $theData));

while ( $i < $LineCount){
$row = $thisArray[$i];
// PARSE BY "-" TO GET HIGH AND LOW NUMBER OF EACH ROW
$thisRow = (explode("-", $row));
$thisRowLowRange = trim($thisRow[0]);
$thisRowHighRange = trim($thisRow[1]);

$m = 0;
for ( $counter = $thisRowLowRange; $counter <= $thisRowHighRange; $counter++)
{
// FOR EACH BOX< MAKE SQL QUERY
$query = "select containerID, stgStatus from storage.container where containerID ='$counter'";
$result = mysqli_query($cxn, $query) or die ("Couldn't execute query");
if(!$result)
{echo "No result:$result";}
while ( $row = mysqli_fetch_assoc($result))
{ 
extract($row);
}
echo "$counter";
if(!strcmp($stgStatus, "Empty")) {
$m++;
print "[/TD][TD]";
} else {
print "[/TD][TD]";
}	
echo "$stgStatus";
echo "[/TD]";
}
echo "[/TABLE]
";
$i++;
}


----------

