How to explode array from file?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • deko

    How to explode array from file?

    contents of myfile.txt = 5035|9638742|11 938 // (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
    reason...

    Is there a better way to do this?

    Thanks in advance.

  • Kim André Akerø

    #2
    Re: How to explode array from file?

    deko wrote:
    contents of myfile.txt = 5035|9638742|11 938 // (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 reason...
    >
    Is there a better way to do this?
    >
    Thanks in advance.
    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(f loatval($var[0]));
    $k = number_format(f loatval($var[1]));

    --
    Kim André Akerø
    - kimandre@NOSPAM betadome.com
    (remove NOSPAM to contact me directly)

    Comment

    • deko

      #3
      Re: How to explode array from file?

      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(f loatval($var[0]));
      $k = number_format(f loatval($var[1]));
      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_fil e, "r");

      while ($timeout < 10 && $gotlock === false)
      {
      if (flock($fp, LOCK_EX))
      {
      $mydata = fread($fp, filesize($data_ file));
      flock($fp, LOCK_UN);
      $gotlock = true;
      }
      else
      {
      usleep(100000);
      }
      $timeout++;
      }

      if ($gotlock)
      {
      [process data]
      }

      seems to be working....


      Comment

      Working...