0

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

SafeY
  • 287
  • 2
  • 6
  • 18

2 Answers2

0

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.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
  • It's working now , thanks , but I need to save edits on the same file ! is there a way to do that ?? – SafeY Nov 18 '12 at 09:41
  • Of course you can to that. Just close both files, delete input file and rename output file to the input file's name. – Pavel Strakhov Nov 18 '12 at 09:43
  • okay, I will do a search for that :D ,,,, Thank you very much :D – SafeY Nov 18 '12 at 09:44
  • I was asked to re-program it without using an external file , I mean edit the same file I want to make changes to it , how can I do it ? I'm so confused because I had learned that file can be opened whether for reading or for writing . any idea ? – SafeY Dec 22 '12 at 07:16
  • File can be opened for both reading and writing using 'r+' mode. – Pavel Strakhov Dec 22 '12 at 11:12
-1
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 !

Jean
  • 762
  • 5
  • 12
  • i realized : fwrite() should be fwrite($h, $new_line); no ? Withtout the "w+" part ? And did you change your code to write the $new_line var ? – Jean Nov 18 '12 at 09:16
  • yep , i did . still there's no change – SafeY Nov 18 '12 at 09:18
  • I'm trying to echo $newline variable !! and there's nothing echoing !! – SafeY Nov 18 '12 at 09:19
  • `$new_line = $line_arr[0] . "=10";` - this will be right if there is only one '=' in each line. But we can't assume that from the question. – Pavel Strakhov Nov 18 '12 at 09:22