title: Write the correct value of a float to a csv file
body: <p>In the csv file, I have lines with this structure : </p>
  
  <pre><code>Name;timestamp;floatValue
  Name;timestamp;floatValue
  Name;timestamp;floatValue
  ...
  </code></pre>
  
  <p>In my code, I check each value in the line, I filter them,then I want to correct the float value by calling the function <code>floatval</code>, because I have wrong values sometimes like : .904 or 2,454 and using this function returns the right formatted one. </p>
  
  <pre><code>    &lt;?php
  
      if (($handle = fopen('file.csv', "r+")) !== false) {
          while (($line = fgetcsv($handle, 1024, ";")) !== false) {
  
            $headers = [
                'name',
                'timestamp',
                'floatValue',
            ];
            $line = array_combine($headers, $line);
            $args = array('name' =&gt; array('filter' =&gt; FILTER_VALIDATE_REGEXP,
                                                        'options' =&gt; array('regexp' =&gt; '/^[a-zA-Z0-9_:.()\/]*$/'), ),
                          'timestamp' =&gt; array('filter' =&gt; FILTER_CALLBACK,
                                                              'options' =&gt; 'validateDate', ),
                           'floatValue' =&gt; array('filter' =&gt; FILTER_VALIDATE_FLOAT),);
      $result = filter_var_array($line, $args);
      $line = correctFloat($line);
      print_r($line);
      }
  
      }
  
      fclose($handle);
  
      function validateDate($date)
      {
        if (!preg_match("/^\d{8}$/", substr($date, 0, 8))) {
            return false;
        }
        if (!checkdate(substr($date, 4, 2), substr($date, 6, 2), substr($date, 0, 4))) {
            return false;
        }
        if (substr($date, 8, 2) &gt; 23 || substr($date, 10, 2) &gt; 59 || substr($date, 12, 2) &gt; 59) {
            return false;
        }
  
        return true;
      }
  
  
  function correctFloat($line){
    $float = $line['floatValue'];
    $value = str_replace(",",".",$float);
    return str_replace($float,floatval($value),$line);
  }
  
  
      ?&gt;
  </code></pre>
  
  <p>Now I'm stuck how to write the correct value inside the file in the right line? </p>
  
  <p>I tried this:</p>
  
  <pre><code>fwrite($handle,implode(";",correctFloat($line)));
  </code></pre>
  
  <p>But it didn't work. <strong>How to re-write each line into the file in the right position with the correct float without losing data?</strong>
  Any idea?</p>
  
  <blockquote>
    <p>:::EDIT:::</p>
  </blockquote>
  
  <p>I also tried to use a temporary file:</p>
  
  <pre><code>$temp = tmpfile();
  fputcsv($temp,correctFloat($line),';');
  fwrite($handle,fread($temp, 1024));
  </code></pre>
  
  <p>but I didn't get a good result. nothing happened inside the file.</p>
  
  <p><strong>Example</strong></p>
  
  <p><em>Input :</em> </p>
  
  <pre><code>Name;timestamp;.23
  Name;timestamp;2,4578
  Name;timestamp;1.23
  </code></pre>
  
  <p><em>Expected output:</em></p>
  
  <pre><code>Name;timestamp;0.23
  Name;timestamp;2.4578
  Name;timestamp;1.23
  </code></pre>
  
  <p>In this example: 2,4578 should be in the output : 2.4578 (I replace the comma with '.' using the <code>function correctFloat($line)</code> above ) </p>
  
  <blockquote>
    <p><strong>New try:</strong></p>
  </blockquote>
  
  <pre><code>$newline = correctFloat(array_combine($headers, $line));
  array_push($newline,chr(10));
  $l = implode(";",$newline);
  fwrite($handle,$l);
  rewind($handle);
  </code></pre>
  
  <p>Only the first value changes and then the file is refreshing without getting out of the loop.</p>