質問編集履歴

5

修正いたしました。

2017/07/22 04:04

投稿

double16k
double16k

スコア16

test CHANGED
File without changes
test CHANGED
@@ -109,3 +109,41 @@
109
109
  のような「"テス,ト1"」というデータが返ってきます。
110
110
 
111
111
  この場合explodeを使ってもきちんと登録できないため、どのようにすればうまくDBに登録できるかアドバイスをいただけないでしょうか。
112
+
113
+
114
+
115
+ --------------------追記--------------------
116
+
117
+ Kosuke_Shibuya 様からのDBの登録の仕方について
118
+
119
+ 私の質問が悪かったので、追記いたします。
120
+
121
+ csvのセルに「,」がなければ("テス,ト1",テスト2,テスト3)のように取得されず
122
+
123
+ (テスト1,テスト2,テスト3)のようになるので、
124
+
125
+ explodeで配列にし「'」をつけて登録はできます。
126
+
127
+
128
+
129
+ 実際セルに「,」がなければ
130
+
131
+ insert into table (test1,test2,test3) VALUE ('テスト1','テスト2','テスト3');
132
+
133
+ で登録可能です。
134
+
135
+
136
+
137
+ 質問したいのは、csvのセルに「,」が付いていた場合("テス,ト1",テスト2,テスト3)になるので
138
+
139
+ explodeで配列にすると
140
+
141
+ array[0] = "テス
142
+
143
+ array[1] = ト1"
144
+
145
+ となってしまうので、これでは登録ができません。
146
+
147
+ csvのセルに「,」があっても
148
+
149
+ array[0] = テスト1 にする方法はないでしょうか。

4

修正いたしました、何度もすいません。

2017/07/22 04:03

投稿

double16k
double16k

スコア16

test CHANGED
File without changes
test CHANGED
@@ -5,6 +5,10 @@
5
5
 
6
6
 
7
7
 
8
+
9
+
10
+
11
+ ```ここに言語を入力
8
12
 
9
13
  //ファイルパス
10
14
 
@@ -51,6 +55,10 @@
51
55
  }
52
56
 
53
57
  zip_close($zip);
58
+
59
+ ```
60
+
61
+
54
62
 
55
63
 
56
64
 

3

コードを再度修正いたしました

2017/07/22 03:45

投稿

double16k
double16k

スコア16

test CHANGED
File without changes
test CHANGED
@@ -22,31 +22,31 @@
22
22
 
23
23
 
24
24
 
25
- while( $entry = zip_read($zip) ) {
25
+ while( $entry = zip_read($zip) ) {
26
26
 
27
27
 
28
28
 
29
- $csv_file = basename(zip_entry_name($entry));
29
+ $csv_file = basename(zip_entry_name($entry));
30
30
 
31
31
 
32
32
 
33
- //zip内のファイルをオープン
33
+ //zip内のファイルをオープン
34
34
 
35
- zip_entry_open($zip, $entry, "r");
35
+ zip_entry_open($zip, $entry, "r");
36
36
 
37
37
 
38
38
 
39
- //オープンしたファイルを読み込む
39
+ //オープンしたファイルを読み込む
40
40
 
41
- $entry_content = zip_entry_read($entry, zip_entry_filesize($entry));
41
+ $entry_content = zip_entry_read($entry, zip_entry_filesize($entry));
42
42
 
43
- $entry_content = mb_convert_encoding($entry_content, "UTF-8", "SJIS-win");
43
+ $entry_content = mb_convert_encoding($entry_content, "UTF-8", "SJIS-win");
44
44
 
45
45
 
46
46
 
47
- zip_entry_close($entry);
47
+ zip_entry_close($entry);
48
48
 
49
- }
49
+ }
50
50
 
51
51
  }
52
52
 

2

コードを修正いたしました。

2017/07/22 03:36

投稿

double16k
double16k

スコア16

test CHANGED
File without changes
test CHANGED
@@ -46,7 +46,7 @@
46
46
 
47
47
  zip_entry_close($entry);
48
48
 
49
-
49
+ }
50
50
 
51
51
  }
52
52
 

1

コードを書いていなかったため再度投稿しました。

2017/07/22 03:35

投稿

double16k
double16k

スコア16

test CHANGED
File without changes
test CHANGED
@@ -1,33 +1,103 @@
1
+ 失礼しました、コードを記入いたします。
2
+
1
3
  PHPでzipファイルをアップロード展開 -> csv読み込み -> DB登録のシステムを作成しています。
2
4
 
3
5
 
4
6
 
5
- zip関数を使い、
6
7
 
7
- $entry_content = zip_entry_read($entry, zip_entry_filesize($entry);
8
8
 
9
- でcsvファイルの取得をしています。
9
+ //ファイルパス
10
10
 
11
+ $path = $tmp_name;
12
+
13
+
14
+
11
- タの内容が「,」区切りの(テスト1,テスト2,テスト3)のような形で取得されます。
15
+ //zipファイルをオプン
16
+
17
+ $zip = zip_open($path);
18
+
19
+
20
+
21
+ if( $zip ){
12
22
 
13
23
 
14
24
 
15
- このデータをDBに登録したいのですが、mysql insert into した場合もちろんエラーが出力され
25
+ while( $entry = zip_read($zip) ) {
16
26
 
17
- ('テスト1','テスト2','テスト3')のように変更しないといけません。
27
+
18
28
 
19
- $item = explode(",", $array);で一旦配列にし、「'」「"」を付加しようと思ったのですが、
29
+ $csv_file = basename(zip_entry_name($entry));
20
30
 
21
- csvデータの一部のセルに「,」があった場合
31
+
22
32
 
23
- ("テス,ト1",テスト2,テスト3)
33
+ //zip内のファイルをオープン
24
34
 
35
+ zip_entry_open($zip, $entry, "r");
36
+
37
+
38
+
25
- "テス,ト1"というデタが返ってきます。
39
+ //オプンしたファイルを読み込む
40
+
41
+ $entry_content = zip_entry_read($entry, zip_entry_filesize($entry));
42
+
43
+ $entry_content = mb_convert_encoding($entry_content, "UTF-8", "SJIS-win");
26
44
 
27
45
 
28
46
 
29
- これだとexplodeで配列から「'」を付加できないのでDBに登録する際なにか良い方法がないでしょうか。
47
+ zip_entry_close($entry);
48
+
49
+
50
+
51
+ }
52
+
53
+ zip_close($zip);
30
54
 
31
55
 
32
56
 
57
+
58
+
59
+
60
+
61
+
62
+
63
+
64
+
65
+
66
+
67
+ ------
68
+
69
+ コピーして修正していますので、構文の書き間違いがあるかもしれませんがエラーはでていません。
70
+
71
+ 4つcsvファイルをzipで圧縮していますが、4つともweb上では見えています。
72
+
73
+ (テスト1,テスト2,テスト3)
74
+
75
+ (テストa,テストb,テストc)
76
+
77
+ (テストq,テストd,テストf)
78
+
79
+ (テストs,テストw,テストz)
80
+
33
- ろしくお願いいたします。
81
+ うな形です。
82
+
83
+
84
+
85
+ $entry_content は、
86
+
87
+ (テスト1,テスト2,テスト3)のような形で取得されます。
88
+
89
+
90
+
91
+ この $entry_content のデータをDBに登録したいのですが、
92
+
93
+ mysqlでinsert intoする場合、データを('テスト1','テスト2','テスト3')のようにする必要があると思います。
94
+
95
+ はじめは、explode関数を使い配列にしてから「'」or「"」を付加使用と思ったのですが、
96
+
97
+ csvファイルのセルに「,」が含まれている場合、
98
+
99
+ $entry_contentの中は、("テス,ト1",テスト2,テスト3)
100
+
101
+ のような「"テス,ト1"」というデータが返ってきます。
102
+
103
+ この場合explodeを使ってもきちんと登録できないため、どのようにすればうまくDBに登録できるかアドバイスをいただけないでしょうか。