How to explode array from file?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
contents of myfile.txt = 5035|9638742|11938 // (one line of text)

$myfile = "/home/path/public_html/myfile.txt";
$totals = file($myfile);
$var = explode("|", $totals[0]);
$i = number_format($var[0]);
$k = number_format($var[1]);

For some reason this is acting funny.  I am getting a null value for $k for some  

Is there a better way to do this?

Thanks in advance.  

Re: How to explode array from file?

deko wrote:

Quoted text here. Click to load it

The number_format() function accepts the given number as a float, but
you're passing a string as the number. What happens if you rather do
the last 2 lines like this?

$i = number_format(floatval($var[0]));
$k = number_format(floatval($var[1]));

Kim André Akerø
(remove NOSPAM to contact me directly)

Re: How to explode array from file?

Quoted text here. Click to load it

That sounds like a good idea and I will implement your suggestion.  Thanks.

But I think the problem I was having was caused by contention for that file.  
Apparently, another process had a lock on it and file($myfile) was not returning  
anything.  So I tried this:

$timeout = 0;
$gotlock = false;
$fp = fopen($data_file, "r");

while ($timeout < 10 && $gotlock === false)
 if (flock($fp, LOCK_EX))
  $mydata = fread($fp, filesize($data_file));
  flock($fp, LOCK_UN);
  $gotlock = true;

if ($gotlock)
    [process data]

seems to be working....

Site Timeline