Advertisement
rfv123

Q42208643/write-the-correct-value-of-a-float-to-a-csv

Feb 14th, 2017
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.30 KB | None | 0 0
  1. <?php // http://stackoverflow.com/questions/42208643/write-the-correct-value-of-a-float-to-a-csv-file
  2.  
  3. // include __DIR__ .'/__bootstrap__.php';
  4.  
  5. /*
  6.  * create test data file in the current directory...
  7.  */  
  8.  
  9.  define('DATA_FILE', 'Q42208643.dat');
  10.  define('DATA_FILE_OUT', 'Q42208643.out.dat');
  11.  
  12.  /*  */
  13.  $f = fopen(DATA_FILE, 'wb');
  14.  fwrite($f, '122222' .';'. time() .';'. '.123'. PHP_EOL);
  15.  fwrite($f, '233333' .';'. time() .';'. '2,345'. PHP_EOL);
  16.  fwrite($f, '344444' .';'. time() .';'. '3.678'. PHP_EOL);
  17.  fclose($f);
  18.  unset($f);
  19.  /* */
  20.  
  21. // setup field names and validiation rules
  22. $headers = ['name',  'timestamp', 'floatValue',];
  23.  
  24. $args = array(
  25.               'sensor_internal_id' => array('filter' => FILTER_VALIDATE_REGEXP,
  26.                                                       'options' => array('regexp' => '/^[a-zA-Z0-9_:.()\/]*$/'), ),
  27.                                                      
  28.               'timestamp' => array('filter' => FILTER_CALLBACK,
  29.                                    'options' => 'validateDate', ),
  30.                                    
  31.                'value' => array('filter' => FILTER_VALIDATE_FLOAT),
  32.         );
  33.  
  34.  
  35. if (($handle = fopen(DATA_FILE, "r+")) !== false) {
  36.  
  37.     $fOut = fopen(DATA_FILE_OUT, "wb");
  38.  
  39.     while (($fields = fgetcsv($handle, 1024, ";")) !== false) {
  40.  
  41.         $fields[2] = str_replace(",", ".", $fields[2]); // ensure float string is valid
  42.  
  43.         $namedFields = array_combine($headers, $fields);
  44.         $result = filter_var_array($namedFields, $args);
  45.  
  46.         $outLine = correctFloat($fields);
  47.         fwrite($fOut, $outLine . PHP_EOL);
  48.     }
  49.     fclose($handle);
  50.     fclose($fOut);
  51. }
  52.  
  53. // show input / output
  54. echo 'Input:', PHP_EOL;
  55. print_r(file_get_contents(DATA_FILE));
  56.  
  57. echo 'Output:', PHP_EOL;
  58. print_r(file_get_contents(DATA_FILE_OUT));
  59.  
  60. exit(0);
  61.  
  62. function validateDate($date)
  63. {
  64.   if (!preg_match("/^\d{8}$/", substr($date, 0, 8))) {
  65.       return false;
  66.   }
  67.   if (!checkdate(substr($date, 4, 2), substr($date, 6, 2), substr($date, 0, 4))) {
  68.       return false;
  69.   }
  70.   if (substr($date, 8, 2) > 23 || substr($date, 10, 2) > 59 || substr($date, 12, 2) > 59) {
  71.       return false;
  72.   }
  73.  
  74.   return true;
  75. }
  76.  
  77.  
  78. function correctFloat($fields) {
  79.   $fields[2] = floatval($fields[2]);
  80.   return implode(';', $fields);
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement