{"id":1979,"date":"2020-09-10T14:15:39","date_gmt":"2020-09-10T05:15:39","guid":{"rendered":"https:\/\/qol-kk.com\/wp2\/?p=1979"},"modified":"2020-09-10T14:44:58","modified_gmt":"2020-09-10T05:44:58","slug":"post-1979","status":"publish","type":"post","link":"https:\/\/qol-kk.com\/wp2\/blog\/2020\/09\/10\/post-1979\/","title":{"rendered":"PHP\u3067CSV\u3092\u8aad\u307f\u8fbc\u3093\u3060\u3089\u914d\u5217\u306b\u7570\u5e38\u304c\u51fa\u3066\u3044\u305f\u4ef6"},"content":{"rendered":"<h2>PHP\u3067CSV\u3092\u8aad\u307f\u8fbc\u3093\u3060\u3089\u914d\u5217\u306b\u7570\u5e38\u304c\u51fa\u3066\u3044\u305f\u4ef6<\/h2>\n<p>\u3053\u306e\u554f\u984c\u77e5\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u3002\u65e2\u77e5\u306e\u554f\u984c\u306a\u306e\u3060\u305d\u3046\u3067\u3059\u3002<\/p>\n<div class=\"pointBox\" style=\"text-align: center;\">SJIS\u306eCSV\u3092fgetcsv\u3067\u76f4\u63a5\u8aad\u307f\u53d6\u308b\u306e\u306f\u300c5C\u554f\u984c\u300d\u306e\u305f\u3081\u5371\u967a\u3067\u3042\u308b\u3002<\/div>\n<h3>\u767a\u751f\u3057\u305f\u7570\u5e38<\/h3>\n<p>\u697d\u5929\u5e02\u5834\u306e\u5546\u54c1\u5206\u6790\u3092CSV\u3067\u843d\u3068\u3057\u3001PHP\u3067\u8aad\u8fbc\u307f\u914d\u5217\u5316\u3001\u6b32\u3057\u3044\u60c5\u5831\u3092DB\u306b\u683c\u7d0d\u3059\u308b\u3068\u8a00\u3046\u4f7f\u3044\u65b9\u3092\u3057\u305f\u306e\u3067\u3059\u304c\u3001\u30ec\u30b3\u30fc\u30c9\u3092\u3088\u304f\u898b\u308b\u3068\u610f\u5473\u4e0d\u660e\u306a\u3082\u306e\u304c...\u3002<\/p>\n<div class=\"borderBox\">\n<p>6\u5217\u76ee\u306e\u5024\u306b\u306f\u51fa\u54c1URL\u304c\u683c\u7d0d\u3055\u308c\u3066\u3044\u308b\u306f\u305a\u306a\u306e\u306b\u3010 0 \u3011\u3068\u3044\u3046\u30ec\u30b3\u30fc\u30c9\u304c\u5b58\u5728\u3059\u308b<\/p>\n<\/div>\n<p>\u697d\u5929\u5e02\u5834\u304b\u3089DL\u3055\u308c\u305f\u30c7\u30fc\u30bf\u306e\u6587\u5b57\u30b3\u30fc\u30c9\u306f Shift_jis \u30b5\u30fc\u30d0\u30fc\u5074\u306f UTF-8 \u306a\u306e\u3067\u3053\u306e\u6587\u5b57\u30b3\u30fc\u30c9\u306e\u554f\u984c\u3060\u308d\u3046\u3068\u601d\u3044\u3001\u884c\u3054\u3068\u306b\u8aad\u8fbc\u3093\u3067\u3044\u308b\u30d1\u30fc\u30c8\u3067\u30a8\u30f3\u30b3\u30fc\u30c9\u3092\u5909\u66f4\u3001\u518d\u5ea6\u5b9f\u884c\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>...\u7d50\u679c\u304c\u5909\u308f\u3089\u306a\u3044\u3002<br \/>\n\u307e\u305a\u3044\u3001\u3053\u308c\u306f\u30cf\u30de\u308b\u30eb\u30fc\u30c8\u304b\u3082...\u3002<\/p>\n<h3>\u307e\u305a\u306f\u4f55\u304c\u8d77\u3053\u3063\u3066\u3044\u308b\u306e\u304b\u78ba\u8a8d<\/h3>\n<p>\u53ef\u80fd\u6027\u3068\u3057\u3066\u306f\u3053\u306e3\u3064\u3057\u304b\u8003\u3048\u3089\u308c\u306a\u3044<\/p>\n<div class=\"paperBox\">\n<ol>\n<li>\u5546\u54c1\u540d\u7b49\u306b\u30ab\u30f3\u30de\u304c\u5165\u3063\u3066\u3044\u3066CSV\u306e\u8aad\u8fbc\u5834\u6240\u304c\u305a\u308c\u3066\u3057\u307e\u3063\u305f<\/li>\n<li>\u4f7f\u3048\u306a\u3044\u6587\u5b57\u304c\u3042\u308b<\/li>\n<li>\u30a8\u30f3\u30b3\u30fc\u30c9\u306e\u5909\u63db\u304c\u4e0a\u624b\u304f\u3044\u3063\u3066\u3044\u306a\u3044<\/li>\n<\/ol>\n<\/div>\n<p>\u307e\u305a\u306f\u30a8\u30f3\u30b3\u30fc\u30c9\u5909\u63db\u7d50\u679c\u306e\u78ba\u8a8d\u3002<br \/>\n\u610f\u5473\u4e0d\u660e\u306a\u6587\u5b57\u306f\u5165\u3063\u3066\u3044\u306a\u3044\u306e\u3067\u30a8\u30f3\u30b3\u30fc\u30c9\u306f\u6210\u529f\u3057\u3066\u3044\u308b\u3088\u3046\u3067\u3059\u3002<br \/>\n\u30ab\u30f3\u30de\u3082\u5165\u3063\u3066\u3044\u306a\u3044\u3002<br \/>\n\u3068\u306a\u308b\u3068\u4f7f\u3048\u306a\u3044\u6587\u5b57\uff1f\uff1f<\/p>\n<p>\u4f7f\u7528\u3057\u3066\u3044\u305f\u6587\u5b57\u3092\u5207\u65ad\u3057\u3066\u30c6\u30b9\u30c8\u3057\u3066\u307f\u305f\u6240\u3001\u6b21\u306e\u73fe\u8c61\u3092\u78ba\u8a8d\u3057\u307e\u3057\u305f\u3002<\/p>\n<div class=\"borderBox\">\n<p style=\"text-align: center;\"><span class=\"markerPink\"><strong>\u6587\u5b57\u5217\u306e\u6700\u5f8c\u306b\u3010\u80fd\u3011\u306e\u6587\u5b57\u304c\u5165\u3063\u3066\u3044\u308b\u3068\u6b21\u306e\u30ab\u30e9\u30e0\u306e\u5024\u304c 0 \u306b\u306a\u308b<\/strong><\/span><\/p>\n<\/div>\n<h3>fgetcsv\u3067\u306eCSV\u8aad\u8fbc\u3067\u306f\u300e5C\u554f\u984c\u300f\u304c\u767a\u751f\u3059\u308b<\/h3>\n<div class=\"exclamationBox\">\n<p>\u4e01\u5ea6\u3010\u53ef\u80fd\u3011\u3063\u3066\u66f8\u3044\u3066\u3042\u308b\u3068\u3053\u308d\u304c\u30c0\u30e1\u3060\u3063\u305f\u3093\u3067\u3059\u3088\u306d\u3002\u25a05C\u554f\u984c\u3068\u306f<\/p>\n<p>\u300c\u30bd\u300d\u300c\u8868\u300d\u300c\u5341\u300d\u300c\u4e88\u300d\u306a\u3069SJIS\u306e\u6587\u5b57\u306e2\u30d0\u30a4\u30c8\u76ee\u304c\u300c5C\u300d\u306b\u306a\u308b\u6587\u5b57\u304c\u3001fgetcsv\u306a\u3069\u306e\u51e6\u7406\u6a5f\u306b\u4e0e\u3048\u3089\u308c\u305f\u3068\u304d\u3001\u305d\u308c\u304c\u6587\u5b57\u306e\u4e00\u90e8\u3068\u3057\u3066\u3067\u306a\u304f\u3001ASCII\u306e\u30d0\u30c3\u30af\u30b9\u30e9\u30c3\u30b7\u30e5(5C)\u3068\u5224\u65ad\u3057\u3066\u3057\u307e\u3044\u3001\u30a8\u30b9\u30b1\u30fc\u30d7\u6587\u5b57\u3068\u3057\u3066\u89e3\u91c8\u3055\u308c\u308b\u554f\u984c\u3092\u8a00\u3046\u3002<\/p>\n<\/div>\n<p>\u306a\u308b\u307b\u3069\u300c\u80fd\u300d\u3082\u3053\u3053\u306b\u5165\u308b\u306e\u306d\u3002<br \/>\n\u4ed6\u306b\u3082\u3053\u3093\u306a\u6587\u5b57\u304c5C\u554f\u984c\u3092\u5f15\u304d\u8d77\u3053\u3059\u3088\u3046\u3067\u3059\u3002<\/p>\n<div class=\"borderBox\">\n<p>\u2014 \u30bd \u042b \u5642 \u6d6c \u6b3a \u572d \u69cb \u8695 \u5341 \u7533 \u66fe \u7baa \u8cbc \u80fd \u8868 \u66b4 \u4e88 \u7984 \u5154 \u5580 \u5abe \u5f4c \u62ff \u6764 \u6b43 \u6fec \u755a \u79c9 \u7db5 \u81c0 \u85f9 \u89f8 \u8ec6 \u9414 \u9945 \u9ded<\/p>\n<\/div>\n<h3>\u5b89\u5168\u306b\u51e6\u7406\u3059\u308b\u65b9\u6cd5<\/h3>\n<p>\u6b21\u306e\u6bb5\u53d6\u308a\u3067\u64cd\u4f5c\u3092\u884c\u3046\u3068\u89e3\u6d88\u3055\u308c\u307e\u3059\u3002<\/p>\n<div class=\"paperBox\">\n<ol>\n<li>file_get_contents\u3067SJIS\u3067\u66f8\u304b\u308c\u305fCSV\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080<\/li>\n<li>\u8aad\u8fbc\u3093\u3060\u30d5\u30a1\u30a4\u30eb\u3092UTF-8\u306b\u30a8\u30f3\u30b3\u30fc\u30c9\u3059\u308b<\/li>\n<li>\u30a8\u30f3\u30b3\u30fc\u30c9\u3057\u305f\u30d5\u30a1\u30a4\u30eb\u3092 file_put_contents \u3067\u4e0a\u66f8\u304d\u4fdd\u5b58\u3059\u308b<\/li>\n<li>\u4e0a\u66f8\u304d\u3057\u305f\u3082\u306e\u3092 fgetcsv \u3067\u8aad\u8fbc\u307f\u5c55\u958b\u3059\u308b<\/li>\n<\/ol>\n<\/div>\n<p>\u3081\u3093\u3069\u304f\u3055\u3044\u3067\u3059\u304c\u3057\u3087\u3046\u304c\u306a\u3044\u3067\u3059\u306d\u3002<\/p>\n<h3>\u5b9f\u969b\u306b\u66f8\u3044\u305f\u30b3\u30fc\u30c9<\/h3>\n<pre class=\"lang:default decode:true  \">#function\u5185\r\n    $filePath = \"hoge\/hoga\/item_list_20200801_day_fuga.csv\";\r\n\r\n    # file -i \u30d5\u30a1\u30a4\u30eb\u540d\u306e\u30b3\u30de\u30f3\u30c9\u3067\u6587\u5b57\u30b3\u30fc\u30c9\u3092\u8abf\u3079\u308b\r\n    $command = \"file -i \" . $filePath ;\r\n    $output = [];\r\n    $status = \"\";\r\n    exec($command, $output, $status);\r\n\r\n    #\u30a8\u30f3\u30b3\u30fc\u30c9\u3092\u629c\u304d\u53d6\u308b\r\n    preg_match(\"\/charset=(.*)\/\", $output[0], $charset);\r\n\r\n    #utf-8\u51fa\u306a\u3044\u5834\u5408\u51e6\u7406\u5b9f\u884c\r\n    if (!in_array(\"utf-8\", $charset)) {\r\n        #\u6587\u5b57\u30b3\u30fc\u30c9\u306e\u5909\u66f4\u203b\u8aad\u8fbc\u3080\u524d\u306b\u5909\u63db\u3057\u3066\u304a\u304f\r\n        $sjis = file_get_contents(filePath);\r\n        $utf8 = mb_convert_encoding($sjis, 'UTF-8', 'Shift_jis');\r\n        file_put_contents($data[0], $utf8);\r\n    }\r\n\r\n    $row = 0;\r\n    \/\/ \u30d5\u30a1\u30a4\u30eb\u5b58\u5728\u30c1\u30a7\u30c3\u30af\r\n    if (($handle = fopen(filePath , \"r\")) !== FALSE) {\r\n        \/\/ 1\u884c\u305a\u3064\u8aad\u8fbc\r\n        $value = [];\r\n        while (($value = fgetcsv($handle))) {\r\n            \/\/\u30ec\u30b3\u30fc\u30c9\u958b\u59cb\u307e\u3067\u7121\u8996\r\n            if(($row &gt;=6)&amp;&amp;(!empty($value[0]))){\r\n                \/\/\u5546\u54c1URL\u3092dump\u3059\u308b\r\n                var_dump(strval($value[5]));\r\n            }\r\n            $row++;\r\n        }\r\n        fclose($handle);\r\n    }<\/pre>\n<h3>\u307e\u3068\u3081<\/h3>\n<p>\u81ea\u5206\u306a\u308a\u306b\u300c\u7d50\u69cb\u304b\u3051\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u304d\u305f\u306a\u300d\u306a\u3069\u3068\u601d\u3063\u3066\u3044\u305f\u77e2\u5148\u306b\u30b3\u30ec\u3067\u3059\u3002<\/p>\n<p>\u307e\u3060\u307e\u3060\u8db3\u308a\u306a\u3044\u77e5\u8b58\u306f\u591a\u3044\u306a\u3041\u3068\u30b7\u30df\u30b8\u30df\u611f\u3058\u307e\u3057\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u3067CSV\u3092\u8aad\u307f\u8fbc\u3093\u3060\u3089\u914d\u5217\u306b\u7570\u5e38\u304c\u51fa\u3066\u3044\u305f\u4ef6 \u3053\u306e\u554f\u984c\u77e5\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u3002\u65e2\u77e5\u306e\u554f\u984c\u306a\u306e\u3060\u305d\u3046\u3067\u3059\u3002 SJIS\u306eCSV\u3092fgetcsv\u3067\u76f4\u63a5\u8aad\u307f\u53d6\u308b\u306e\u306f\u300c5C\u554f\u984c\u300d\u306e\u305f\u3081\u5371\u967a\u3067\u3042\u308b\u3002 \u767a\u751f\u3057\u305f\u7570\u5e38 \u697d\u5929\u5e02\u5834\u306e\u5546\u54c1\u5206\u6790 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":944,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"vkexunit_cta_each_option":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[34],"tags":[734,733,737,738,739,736,99,98,97,735,53,112,45],"class_list":["post-1979","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-memorandum","tag-5c","tag-csv","tag-fgetcsv","tag-file_get_contents","tag-file_put_contents","tag-mb_convert_encoding","tag-mysql","tag-php","tag-97","tag-735","tag-53","tag-112","tag-45"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/qol-kk.com\/wp2\/wp-content\/uploads\/2018\/11\/laravel_logo.png","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts\/1979","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/comments?post=1979"}],"version-history":[{"count":0,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts\/1979\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/media\/944"}],"wp:attachment":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/media?parent=1979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/categories?post=1979"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/tags?post=1979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}