Last active
May 3, 2016 13:44
-
-
Save bzz0217/5c249a1e8121956fa0a03a0185a2f60d to your computer and use it in GitHub Desktop.
CSVを取得し配列に変換
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class get_csv{ | |
public $file; | |
public $from_encoding; | |
/* | |
* @param string filepath ファイルパス | |
* @param string from_encoding CSVファイルの文字コード | |
*/ | |
public function __construct($filepath, $from_encoding = "SJIS"){ | |
$this->file = new SplFileObject($filepath); | |
$this->file->setFlags(SplFileObject::READ_CSV); | |
$this->from_encoding = $from_encoding; | |
} | |
/* | |
* CSVを配列に変換 | |
* @return array records CSV配列 | |
*/ | |
public function csv_array_convert(){ | |
foreach ($this->file as $line){ | |
if(!is_null($line[0])){ | |
mb_convert_variables("UTF-8", $this->from_encoding, $line); | |
$records[] = $line; | |
} | |
} | |
return $records; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment