Click to See Complete Forum and Search --> : exploding by comma. what if there is no comma?


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

?>

bokeh
01-09-2006, 11:19 AM
If the delimiter is not present no error will be raised by explode(). All that will happen is the whole string will be loaded into array key '0'. In the case of your script though it is strpos() that is raising the error. Without knowing what $buffer and $node contain it is impossible to know what is causing the error. Try echoing both to see if they contain what you think they should.

Ubik
01-09-2006, 11:26 AM
You can test if it is an array first [is_array($this)], or add a blank record on each end before you explode it, then ignore the blank record when you process the data later.

towerboy
01-09-2006, 11:29 AM
OK thanks. I will try this when i get a chance.

bokeh
01-09-2006, 12:03 PM
You can test if it is an array first [is_array($this)], explode will always make an array, it's just that if the delimiter isn't present the array will only have one entry. Even with just one entry it is still an array and will return true to your code.

towerboy
01-09-2006, 06:34 PM
I did some messing with the script and wasn't having any success so I put everything back the way I 'thought' it used to be. Not sure what I did but it works now. How embarassing..... Thanks for your help anyway. :)