Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Manipulating tabbed data from a log file

Status
Not open for further replies.

s0crates9

Technical User
Jun 18, 2005
70
US
Essentially what I am trying to achieve is to collect data from a custom log file. I already created an apache log that reports data like this:

siteip sitename pagename

I am trying to collect line by line these elements and I am not sure how to grab them first by line in a text file and second manipulate each element on a line (which is tabbed with php's \t". Can anyone explain or show me how this can easily be done?

Web site design, internet marketing, SEO and business solutions company.
 
try this

Code:
$file = "" ; //filename
$fh = fopen($file, "r")
  or die("Unable to open file");
while ($data = fgetcsv($fh, 2048, "\t")):
	print_r($data);
        echo "<br/>";
endwhile;

clearly you can replace the inside of the while loop with whatever you want. you might shove it into an array and then manipulate afterwards:

Code:
while ($data[] = fgetcsv($fh, 2048, "\t")):
//do nothing
endwhile;
//output is in the array $data
 
Thanks Jpadie,

It worked very well. There is one thing though. I wanted to be able to control the lines and right now it prints the arrays and not by the ip which is held in $data[0].
each row of data has 12 fields so I will experiement with breaking down the array, but I am not sure if there is a short and sweet way of doing so, and I would appreciate it if you do have some insight on this.

Thanks so much for the example, it works in the way I wanted the data!

Web site design, internet marketing, SEO and business solutions company.
 
not entirely sure what you mean - do you want the array sorted or just don't want the print structure?

assuming the latter you need the second example:

Code:
$file = "" ; //filename
$fh = fopen($file, "r")
  or die("Unable to open file");
echo "<table>";
while ($data = fgetcsv($fh, 2048, "\t")):
  echo "<tr>";
  foreach ($data as $val):
    echo "<td>".htmlspecialchars($val)."</td>";// the html specialchars function may not be needed here - depends on the contents of the data.
  endforeach;
  echo "</tr>";
endwhile;
echo "</table>";
 
I actually accomplished what I wanted. Thanks Jpadie. I made the array into manageable chunks by modifying the code you supplied by adding a for loop.

This way I could have it limited by 12 (which is the total amount of data delimited by commas) and this occurred for each row!

Thanks for the assistance!

Web site design, internet marketing, SEO and business solutions company.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top