简体   繁体   中英

How to explode a line in text file and write a new value for second part of it using PHP

I'm trying to change values in atext file after "=", explode returns an array of two elements, I want the second element to be replaced with a new value and then written in my file, but I'm lost with syntax !!

 $trID_Log_File = $fileName;
 if(file_exists($trID_Log_File) && filesize($trID_Log_File) > 0) 
   {    
    $h = fopen($trID_Log_File, "r");
    $contents = fread($h, filesize($trID_Log_File));
    fclose($h);

    if(!stristr($contents, "TrID is best suited to analyze binary files!")) 
    {

        $lines = explode("\n", $contents);
        foreach($lines as $line) 
        {
            if(strlen($line) > 5) 
            {
                $line_arr=explode("=",$line);

                                    if ($line_arr[0]=='Sally')
                                    {
                  $line_arr[1]="10"; // The New Value // ??????
                                       fwrite($h,$line_arr,"w+")     ; // ?????????
                                   }
            }
                 }
     }
   }

input :

sally= 10
samy=40

desired Output :

sally=55
samy=123 

what's the right syntax for this problem !! am I missing some code ?? thanks

Here is a working code:

<?php
if(file_exists($trID_Log_File) && filesize($trID_Log_File) > 0)  {    
  $h = fopen($trID_Log_File, "r");
  $contents = fread($h, filesize($trID_Log_File));
  fclose($h);
  $out_h = fopen("output filename", "w");    
  if(!stristr($contents, "TrID is best suited to analyze binary files!")) {
    $lines = explode("\n", $contents);
    foreach($lines as $line) {
      if(strlen($line) > 5) {
        $line_arr=explode("=",$line);      
        if ($line_arr[0]=='Sally') {
          $line_arr[1]="10"; // The New Value 
        }
        fwrite($out_h, implode("=", $line_arr)."\n"); 
      }
    }
  }
}

There are several mistakes in your code. First, you're trying to write an array to file, but you should use implode function to convert it back to a string with = separator.

Second, you're using fwrite completely wrong. The first argument of this function must be opened file handler, but you've passed closed one. The second parameter must be a string to write, you've passed an array. The third optional argument is length, but you've passed "w+", this doesn't make any sense for fwrite. fwrite cannot open files, so it cannot work with file access modifiers.

Third, you can't modify the file in place to implement your task. If you would have to do this, you'd have to set file cursor to the start of each line, then overwrite the line with new contents. And if the length of new line is not equal to the length of old line, it becomes very complicated. So, you should create another output file and write all output to it.

There are some other issues in the code I've posted:

  • This will not work for big files. If it's supposed to, you must not load entire file to the memory. Read a line using fgets , process it, write it to new file, and then read the next line.
  • The lines that don't contains 'Sally' will not be included in the output file. You may want to change this behavior.

Also, I didn't check the stristr condition, so I don't know if it works or not.

And all of this is not about syntax. The interpreter tells you about syntax errors very specifically if there are any.

if(strlen($line) > 5) {
$line_arr=explode("=",$line);

if ($line_arr[0]=='Sally'){
    $line_arr[1]="10"; // The New Value // ??????
    $new_line = implode("=", $line_arr); //reconstruct the string here
    // or something like that :
    // $new_line = $line_arr[0] . "=10";
    fwrite($h,$new_line)     ; // ?????????
}
}

Use implode on your array before writing to file. If not, you are trying to write an array to the file... you must reconstruct the string before !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM