towerboy
01-09-2006, 08:19 AM
Hi,
I am working at getting a small script running and am almost done I think. The problem I am having now is this.
The script opens a text file called nodes.txt and the contents of that file are returned as $buffer. The file is comma delimted The next line in my script is this.
$buf_arr = explode(",", $buffer);
What if there are no commas in the file? If there is only one node listed and there is not any comma to separate it from anything else, the browser returns a lot of these.
Warning: strpos() [function.strpos]: Empty delimiter. in C:\Apache2\htdocs\irlp\irlp_nodes.php on line 25
How do I tell the script that if there one node listed in the file to not look for the comma?
Here is the code of the script I am working on.
<?PHP
// gets remote file and saves locally
$text = file_get_contents('http://localhost/status/nohtmlstatus.txt') or
die("Couldn't read remote file");
$handle = fopen("localstatus.txt", "w") or die("Unable to open to local file.");
fwrite($handle, $text) or die("Unable to write to file");
fclose($handle);
// opens node list and saves to $buffer
$handle = fopen('nodes.txt' , "r");
$buffer = fgets($handle, 5000);
fclose($handle);
$buf_arr = explode(",", $buffer);
$buf_arr["0"];
// for every node number found in $buf_arr, the script runs this loop
foreach( $buf_arr as $node){
// Opens the local file and serches for the requested node number
$handle = fopen('localstatus.txt' , "r");
while(!feof($handle)) {
$buffer = fgets($handle, 4096);
$pos = strpos($buffer, $node);
if ($pos !== FALSE && $pos == '0'){break;}
}
// Closes the file
fclose($handle);
// Sorts the gathered data
$buf_arr = explode("\t", $buffer);
// Assigns the status of the node to a string
$status = $buf_arr['5'];
// Assigns the node callsign to a string
$callsign = $buf_arr['1'];
// Assembles first part of output
$own_node = ($callsign . ' Node ' . $node);
// Based on what the status was, the final output and color are being set
switch ($status) {
case 'BUSY';
$node_status = $own_node . ' is ' . $status;
$color = '#FF0000';
break;
case 'OFFLINE';
$node_status = $own_node . ' is ' . $status;
$color = '#FF0000';
break;
case 'DOWN';
$node_status = $own_node . ' is ' . $status;
$color = '#FF0000';
break;
case 'IDLE';
$node_status = ($own_node . ' is ' . $status);
$color = '#00FF00';
break;
default:
if ($status < '9000') {
$node_status = $own_node . ' is connected to Node ' . $status;
$color = '#FF0000';
}
elseif ($status > '8999') {
$node_status = $own_node . ' is connected to Reflecter ' . $status;
$color = '#FFFF00';
}
break;
}
// Verifies the node number proccessed is the same node thatwas requested
$verify = $buff_arr['0'];
if ($verify != $node) {
$node_status = 'Sorry, there was no data found for Node ' . $node;
$color = '#33CCFF';
}
// builds the output of any combined nodes
$final_output .= "<font color=$color>";
$final_output .= "$node_status :: ";
$final_output .= $buf_arr['2'];
$final_output .= " :: ";
$final_output .= $buf_arr['3'];
$final_output .= "</font><br>";
}
?>
I am working at getting a small script running and am almost done I think. The problem I am having now is this.
The script opens a text file called nodes.txt and the contents of that file are returned as $buffer. The file is comma delimted The next line in my script is this.
$buf_arr = explode(",", $buffer);
What if there are no commas in the file? If there is only one node listed and there is not any comma to separate it from anything else, the browser returns a lot of these.
Warning: strpos() [function.strpos]: Empty delimiter. in C:\Apache2\htdocs\irlp\irlp_nodes.php on line 25
How do I tell the script that if there one node listed in the file to not look for the comma?
Here is the code of the script I am working on.
<?PHP
// gets remote file and saves locally
$text = file_get_contents('http://localhost/status/nohtmlstatus.txt') or
die("Couldn't read remote file");
$handle = fopen("localstatus.txt", "w") or die("Unable to open to local file.");
fwrite($handle, $text) or die("Unable to write to file");
fclose($handle);
// opens node list and saves to $buffer
$handle = fopen('nodes.txt' , "r");
$buffer = fgets($handle, 5000);
fclose($handle);
$buf_arr = explode(",", $buffer);
$buf_arr["0"];
// for every node number found in $buf_arr, the script runs this loop
foreach( $buf_arr as $node){
// Opens the local file and serches for the requested node number
$handle = fopen('localstatus.txt' , "r");
while(!feof($handle)) {
$buffer = fgets($handle, 4096);
$pos = strpos($buffer, $node);
if ($pos !== FALSE && $pos == '0'){break;}
}
// Closes the file
fclose($handle);
// Sorts the gathered data
$buf_arr = explode("\t", $buffer);
// Assigns the status of the node to a string
$status = $buf_arr['5'];
// Assigns the node callsign to a string
$callsign = $buf_arr['1'];
// Assembles first part of output
$own_node = ($callsign . ' Node ' . $node);
// Based on what the status was, the final output and color are being set
switch ($status) {
case 'BUSY';
$node_status = $own_node . ' is ' . $status;
$color = '#FF0000';
break;
case 'OFFLINE';
$node_status = $own_node . ' is ' . $status;
$color = '#FF0000';
break;
case 'DOWN';
$node_status = $own_node . ' is ' . $status;
$color = '#FF0000';
break;
case 'IDLE';
$node_status = ($own_node . ' is ' . $status);
$color = '#00FF00';
break;
default:
if ($status < '9000') {
$node_status = $own_node . ' is connected to Node ' . $status;
$color = '#FF0000';
}
elseif ($status > '8999') {
$node_status = $own_node . ' is connected to Reflecter ' . $status;
$color = '#FFFF00';
}
break;
}
// Verifies the node number proccessed is the same node thatwas requested
$verify = $buff_arr['0'];
if ($verify != $node) {
$node_status = 'Sorry, there was no data found for Node ' . $node;
$color = '#33CCFF';
}
// builds the output of any combined nodes
$final_output .= "<font color=$color>";
$final_output .= "$node_status :: ";
$final_output .= $buf_arr['2'];
$final_output .= " :: ";
$final_output .= $buf_arr['3'];
$final_output .= "</font><br>";
}
?>