PHP - How to explode () strings from a text file into an array?

This is what I am trying to do:

  • I have a member.log text file
  • ADD TOTAL the amount of outstanding payments from each member-210.00, etc. For example: inactive: [2007-04-01 08:42:21] "home / club / member" 210.00 "r-200"

  • It seems to me that I will need to separate the different parts of the post so that I can target the [key] that matches the amount of 210.00, etc.

  • I thought to do this using explode (), but since I am not passing a string to explode (), I get an error: Warning: explode () expects parameter 2 to be the array given in / home / mauri 210 / public_html /lfctribe.com/index.php on line 25

How can I solve this problem to add the grand total for each row?

Here is my php:

<?php
//Open the dir 
$dirhandle = opendir('/home/mauri210/public_html/lfctribe.com/data');

//Open file 
$file = fopen('/home/mauri210/public_html/lfctribe.com/data/members.log', 'r');

//Declare array 
$arrFile = array();

//Add each line of file in to array while not EOF 
while (!feof($file)) {
    $arrFile[] = fgets($file);

    //explode 
    $exarrFile = explode(' ', $arrFile);
}

var_dump($exarrFile);
?>

      

Here are the contents of members.log:

inactive : [2007-04-01 08:42:21] "home/club/member" 210.00 "r-200"
inactive : [2008-08-01 05:02:20] "home/club/staff" 25.00 "r-200"
active : [2010-08-11 10:12:20] "home/club/member" 210.00 "r-500"
inactive : [2010-01-02 11:12:33] "home/premier/member" 250.00 "r-200"
active : [2013-03-04 10:02:30] "home/premier/member" 250.00 "r-800"
active : [2011-09-14 15:02:55] "home/premier/member" 250.00 "r-100"

      

+3


source to share


4 answers


    while (!feof($file)) {
        $arr_file = fgets($file);
        $arrFile[] = fgets($file);

        //explode 
        $exarrFile = explode(' ', $arr_file);
    }
var_dump($exarrFile);

      



+1


source


Try something like this



  $sum=0;
  foreach(file("path/to/file") as $line )
  {
      $fields=explode (" ", $line);
      $sum += $fields[count($fields)-1];
   }
      echo $sum;

      

+1


source


You will need this I think

$items= preg_split('/[,\s]+/', $yourline);

      

0


source


I think I solved this problem. I tested with a small amount of sample data and it seems to work. Here is my updated code:

    <?php


//Open the dir 
    $dirhandle = opendir('/home/mauri210/public_html/lfctribe.com/data');

//Open file 
    $file = fopen('/home/mauri210/public_html/lfctribe.com/data/members.log', 'r');

//List contents of file 

    while (($contents = fgets($file)) !== false) {

    $parts = explode(" ", $contents);
    $total = $total + $parts[5];
    }

    var_dump($parts);

    echo "this is key 5 $total";
?>

      

0


source







All Articles