daily pastebin goal
62%
SHARE
TWEET

Untitled

a guest Jun 14th, 2018 68 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. <?php
  2. /**
  3. * readcsv.php
  4. */
  5.  
  6. if(!isset($argv[1])){
  7.   echo "php {$argv[0]} csvfile";
  8.   exit;
  9. }
  10.  
  11. $handle = @fopen($argv[1], 'r');
  12. $flug = false;
  13. $buff = null;
  14. while($line = fgets($handle)){
  15.   $line = mb_convert_encoding($line, 'UTF-8', 'Shift_JIS');
  16.  
  17.   $temp = str_replace('""', '', $line);
  18.   $temp = str_replace('\"', '', $temp);
  19.  
  20.   //改行処理
  21.   if(!$flug){
  22.     if(substr_count($temp, '"')%2 == 1){
  23.       $buff = $line;
  24.       $flug = true;
  25.       continue;
  26.     }
  27.   } else {
  28.     if(substr_count($temp, '"')%2 == 1){
  29.       $line = $buff . $line;
  30.       $flug = false;
  31.       $buff = null;
  32.     } else {
  33.       $buff .= $line;
  34.       continue;
  35.     }
  36.   }
  37.  
  38.   //分割
  39.   $csv = explode(",", $line);
  40.  
  41.   $data = null;
  42.   $buff = null;
  43.   foreach($csv as $value){
  44.     if(!$flug){
  45.       if(substr_count($value, '"')%2 == 1){
  46.         $buff = $value;
  47.         $flug = true;
  48.         continue;
  49.       }
  50.     } else {
  51.       if(substr_count($value, '"')%2 == 1){
  52.         $value = $buff . ','.$value;
  53.         $flug = false;
  54.         $buff = null;
  55.       } else {
  56.         $buff .= ','.$value;
  57.         continue;
  58.       }
  59.     }
  60.  
  61.     $data[] = trim($value);
  62.   }
  63.  
  64.   var_dump($data);
  65. }
  66.  
  67. fclose($handle);
  68.  
  69. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top