Hi! Thanks for your code ! 🙂
I've tried to use it to find a solution, but I didnt get it in the last 2 days :queasy:
The problem is, that I want to use an older script I've already wrote which bases on the following structure:
<?
$fp = fopen('netstat.txt', 'r');
while(!feof($fp))
{
$line = fgets($fp,1000);
$data = explode(" ", trim($line));
$rows[]=$data;
}
echo "<table border=1>";
foreach($rows as $data)
{
echo "<tr><td>$data[0]</td>
<td>$data[1]</td>
<td>$data[2]</td>
<td>$data[3]</td>
<td>$data[4]</td></tr>";
}
echo "</table>";
fclose($fp);
?>
I want to use this because all the rest of my code bases on this structure (options to find special keywords in the table, a option to rearrange the table-content by column name and so on).
But for now, I cant use this structure because
$data = explode(" ", trim($line));
dont work anymore (because my new file has a different amount of spaces between the informations, as we already know).
So I've tried to use your code to get away these spaces 😉
<?php
$fp = fopen('netstat.txt', 'r');
while(!feof($fp))
{
$input = fgets($fp,1000);
$column_headers = array(
'Aktive',
'Verbindungen',
'Proto',
'Lokale',
'Adresse',
'Remoteadresse',
'Status',
'PID',
);
//Split-up our array by ANY "blank space" character (this
//includes characters like spaces and newlines).
$chunks = split("[[:space:]]", $input);
//Remove any unnecessary elements from the resultant array.
//We'll remove any of our $column_headers as well as any
//empty values (which the spaces from the original input created).
foreach ($chunks as $k=>$v) {
if ($v == '') {
unset($chunks[$k]);
}
else {
foreach ($column_headers as $value) {
if ($v == $value) {
unset($chunks[$k]);
}
}
}
}
//Copy whatever is left over to a new array with correct indeces.
foreach ($chunks as $v) {
$output[] = $v;
}
} // end while
//Set a newline string so we know where to insert newlines.
$newline_string = 'TCP' or 'UDP';
//Iterate through the $output array and print the text.
for ($i = 0; $i < count($output); $i++) {
//Insert a newline if this value matches our $newline_string.
if ($output[$i] == $newline_string) {
$string .= "\r\n";
}
$string .= $output[$i];
//Don't put a tab after the last line, but put one everywhere else.
if ($i != (count($output) - 1)) {
$string .= " ";
}
}
fclose($fp);
Okay, now all my data is in my $string, seperated with one space.
Now I want to use my old code to display every single line in a table.
But this code wouldn't work (because of the "fgets"):
$line = fgets($string,1000);
$data = explode(" ", trim($line));
$rows[]=$data;
echo "<table border=1>";
foreach($rows as $data)
{
echo "<tr><td>$data[0]</td>
<td>$data[1]</td>
<td>$data[2]</td>
<td>$data[3]</td>
<td>$data[4]</td></tr>";
}
echo "</table>";
?>
Any new ideas/hints for me? How can i combine your script with my old one ?
€: well, thats what my old code displays:
http://home.arcor.de/r3in3rs/info/php.JPG
maybe I can use my old code with a code like:
if (column = blank)
{
replace it with next column
}
but how would this look like 😕 Next time I choose something easier 😉