21
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHP、文字化けせずにCSVファイルを読み込み

Last updated at Posted at 2014-07-08

PHP、文字化けせずにCSVファイルを読み込み

■about

PHP、文字化けせずにCSVファイルを読み込み

htmlタグが文字化けするのでHTML エンティティに変換
表示する時にHTML エンティティのデコードする

■code

php
//excel csv、文字コード変換
setlocale(LC_ALL, 'ja_JP.UTF-8');
 
$file = $file_neme . ".csv";
$data = file_get_contents($file);
$data = mb_convert_encoding($data, 'UTF-8', 'sjis-win');

$temp = tmpfile();
$csv  = array();
 
fwrite($temp, $data);
rewind($temp);
 
while (($data = fgetcsv($temp, 0, ",")) !== FALSE) {
$data = implode(",", $data);
//htmlタグが文字化けするのでHTML エンティティに変換
//表示する時にHTML エンティティのデコードする
  $csv[] = htmlentities($data);
}
fclose($temp);
//var_dump($csv);
php
//表示する時にHTML エンティティのデコードする
echo html_entity_decode($line);

■reference

[PHP]文字化けせずにCSVファイルを読み込み、配列に変換する | PHP Archive
http://php-archive.net/php/csv-tsv-array/

PHP: mb_convert_encoding - Manual
http://www.php.net/manual/ja/function.mb-convert-encoding.php

PHP: htmlentities - Manual
http://www.php.net/manual/ja/function.htmlentities.php

PHP: html_entity_decode - Manual
http://www.php.net/manual/ja/function.html-entity-decode.php

21
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?