01647

ustreamer-01647

phpとexplodeと改行コードCRLF

レコード分解時にtrim()する必要なんてなかった.

a.php

<?php
$incsv = file_get_contents("data.txt");
$records = explode("\r\n", trim($incsv));
//$records = explode("\n", trim($incsv));
$r = explode(",", $records[0]);
var_dump($r);

data.csv

UTF-8.改行はCR+LF

a,1,2,3,4
4,5,6,7,8
3,5,6,7,7

出力

$records = explode("\r\n", trim($incsv));

D:\>php a.php
array(5) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "1"
  [2]=>
  string(1) "2"
  [3]=>
  string(1) "3"
  [4]=>
  string(1) "4"
}

$records = explode("\n", trim($incsv));

D:\>php a.php
array(5) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "1"
  [2]=>
  string(1) "2"
  [3]=>
  string(1) "3"
  [4]=>
" string(2) "4
}