質問編集履歴

3

追記

2019/10/24 10:56

投稿

asapan
asapan

スコア60

test CHANGED
File without changes
test CHANGED
@@ -106,7 +106,7 @@
106
106
 
107
107
  ⇒以下の画像の通り、テキストエディタで開いても半角スペースが二つ入ってしまっています。また、バイナリエディタで見たところやはり20(半角スペース)が入っておりました。
108
108
 
109
- ![イメージ説明](b90882cb70f465cdaf9c34024901fe86.jpeg)
109
+ ![イメージ説明](901fc039e070f05219726d986d8a735a.jpeg)
110
110
 
111
111
 
112
112
 

2

追記

2019/10/24 10:56

投稿

asapan
asapan

スコア60

test CHANGED
File without changes
test CHANGED
@@ -182,6 +182,174 @@
182
182
 
183
183
 
184
184
 
185
-
186
-
187
185
  他に何か解決方法は考えられますでしょうか。
186
+
187
+
188
+
189
+
190
+
191
+ 【追記②】
192
+
193
+
194
+
195
+ php.iniを調べてみました。
196
+
197
+
198
+
199
+ ```
200
+
201
+ ; PHP's default character set is set to UTF-8.
202
+
203
+ ; http://php.net/default-charset
204
+
205
+ default_charset = "UTF-8"
206
+
207
+ ```
208
+
209
+
210
+
211
+ ```
212
+
213
+ [mbstring]
214
+
215
+ ; language for internal character representation.
216
+
217
+ ; This affects mb_send_mail() and mbstrig.detect_order.
218
+
219
+ ; http://php.net/mbstring.language
220
+
221
+ ;mbstring.language = Japanese
222
+
223
+
224
+
225
+ ; Use of this INI entry is deprecated, use global internal_encoding instead.
226
+
227
+ ; internal/script encoding.
228
+
229
+ ; Some encoding cannot work as internal encoding. (e.g. SJIS, BIG5, ISO-2022-*)
230
+
231
+ ; If empty, default_charset or internal_encoding or iconv.internal_encoding is used.
232
+
233
+ ; The precedence is: default_charset < internal_encoding < iconv.internal_encoding
234
+
235
+ ;mbstring.internal_encoding =
236
+
237
+
238
+
239
+ ; Use of this INI entry is deprecated, use global input_encoding instead.
240
+
241
+ ; http input encoding.
242
+
243
+ ; mbstring.encoding_traslation = On is needed to use this setting.
244
+
245
+ ; If empty, default_charset or input_encoding or mbstring.input is used.
246
+
247
+ ; The precedence is: default_charset < intput_encoding < mbsting.http_input
248
+
249
+ ; http://php.net/mbstring.http-input
250
+
251
+ ;mbstring.http_input =
252
+
253
+
254
+
255
+ ; Use of this INI entry is deprecated, use global output_encoding instead.
256
+
257
+ ; http output encoding.
258
+
259
+ ; mb_output_handler must be registered as output buffer to function.
260
+
261
+ ; If empty, default_charset or output_encoding or mbstring.http_output is used.
262
+
263
+ ; The precedence is: default_charset < output_encoding < mbstring.http_output
264
+
265
+ ; To use an output encoding conversion, mbstring's output handler must be set
266
+
267
+ ; otherwise output encoding conversion cannot be performed.
268
+
269
+ ; http://php.net/mbstring.http-output
270
+
271
+ ;mbstring.http_output =
272
+
273
+
274
+
275
+ ; enable automatic encoding translation according to
276
+
277
+ ; mbstring.internal_encoding setting. Input chars are
278
+
279
+ ; converted to internal encoding by setting this to On.
280
+
281
+ ; Note: Do _not_ use automatic encoding translation for
282
+
283
+ ; portable libs/applications.
284
+
285
+ ; http://php.net/mbstring.encoding-translation
286
+
287
+ ;mbstring.encoding_translation = Off
288
+
289
+
290
+
291
+ ; automatic encoding detection order.
292
+
293
+ ; "auto" detect order is changed according to mbstring.language
294
+
295
+ ; http://php.net/mbstring.detect-order
296
+
297
+ ;mbstring.detect_order = auto
298
+
299
+
300
+
301
+ ; substitute_character used when character cannot be converted
302
+
303
+ ; one from another
304
+
305
+ ; http://php.net/mbstring.substitute-character
306
+
307
+ ;mbstring.substitute_character = none
308
+
309
+
310
+
311
+ ; overload(replace) single byte functions by mbstring functions.
312
+
313
+ ; mail(), ereg(), etc are overloaded by mb_send_mail(), mb_ereg(),
314
+
315
+ ; etc. Possible values are 0,1,2,4 or combination of them.
316
+
317
+ ; For example, 7 for overload everything.
318
+
319
+ ; 0: No overload
320
+
321
+ ; 1: Overload mail() function
322
+
323
+ ; 2: Overload str*() functions
324
+
325
+ ; 4: Overload ereg*() functions
326
+
327
+ ; http://php.net/mbstring.func-overload
328
+
329
+ ;mbstring.func_overload = 0
330
+
331
+
332
+
333
+ ; enable strict encoding detection.
334
+
335
+ ; Default: Off
336
+
337
+ ;mbstring.strict_detection = On
338
+
339
+
340
+
341
+ ; This directive specifies the regex pattern of content types for which mb_output_handler()
342
+
343
+ ; is activated.
344
+
345
+ ; Default: mbstring.http_output_conv_mimetype=^(text/|application/xhtml+xml)
346
+
347
+ ;mbstring.http_output_conv_mimetype=
348
+
349
+ ```
350
+
351
+ mbstring関係は全てコメントアウトされていました。また、
352
+
353
+ ;extension=php_mbstring.dll
354
+
355
+ ともなっておりました。

1

ご質問の回答追加

2019/10/24 10:53

投稿

asapan
asapan

スコア60

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,7 @@
1
+ (追記いたしました、よろしくお願いいたします。)
2
+
3
+
4
+
1
5
  PHPでファイル書き込み(csv出力)の実装をしています。
2
6
 
3
7
  そこで、なぜかタイトルの通り、出力をされたファイルをテキスト形式(.txt)でみると最初の行の最初に半角スペースが入ってしまっています。
@@ -71,3 +75,113 @@
71
75
 
72
76
 
73
77
  ![イメージ](5a3feff2342351fed9084ac8881c596a.jpeg)
78
+
79
+
80
+
81
+
82
+
83
+ 【追記】
84
+
85
+
86
+
87
+ 皆さまご回答ありがとうございます。
88
+
89
+
90
+
91
+ 出力する内容を隠すためにサンプルの内容を入れたり多少変数名を変えたりしておりまして、
92
+
93
+ その際コード全体として不自然になってしまいました。そのまま投稿したこと申し訳ありません。
94
+
95
+ 以下、その部分も補足しつつご質問の回答と、状態を書きたいと思います。
96
+
97
+
98
+
99
+
100
+
101
+ **ご質問内容⓵**
102
+
103
+ 「テキストエディタで開くとどうなってますか?もしかしたらバイナリエディタのほうがいいかもしれません?」
104
+
105
+
106
+
107
+ ⇒以下の画像の通り、テキストエディタで開いても半角スペースが二つ入ってしまっています。また、バイナリエディタで見たところやはり20(半角スペース)が入っておりました。
108
+
109
+ ![イメージ説明](b90882cb70f465cdaf9c34024901fe86.jpeg)
110
+
111
+
112
+
113
+
114
+
115
+
116
+
117
+ **ご質問内容②**
118
+
119
+ 「ひょっとして、phpタグの前になんかはいってませんかね?」
120
+
121
+
122
+
123
+ ⇒確認いたしましたが、PHPタグの前には何も入っておりませんでした。
124
+
125
+
126
+
127
+
128
+
129
+
130
+
131
+ **ご質問内容③**
132
+
133
+ 「ファイルをバイナリエディタで開き、ゴミがどういったコードなのか調べるところからですね
134
+
135
+ また、多バイト文字ではなくシングルバイト文字ではどうなるか追記ください」
136
+
137
+
138
+
139
+ ⇒上記の通り、バイナリ表示で20,半角スペースが入っておりました。シングルバイト文字でやってみましたが結果は変わらずでした。
140
+
141
+
142
+
143
+
144
+
145
+ **ご質問内容④**
146
+
147
+ 「そのファイルがインクルードしたファイルのどこかで <?php の前か?> のあとに空白や改行、タブなどが入っていないか
148
+
149
+ ファイルをBOMつきで保存してないか」
150
+
151
+
152
+
153
+ ⇒確認しましたが、そのようなことはありませんでした。
154
+
155
+
156
+
157
+ **ご質問内容⑤**
158
+
159
+ 「phpのコードなのに「<?php」が書いていないことから、
160
+
161
+ 一連の処理の一部分のコードを切り出したものだと推察します。
162
+
163
+
164
+
165
+ そして、掲載された箇所のコードでは$dataListの初期化が行われていないことが気になります。
166
+
167
+ ですが、すでに何らかのデータが含まれているのであれば
168
+
169
+ 「あ」がセルA1の位置に来ない気がするのでしっくりきません。」
170
+
171
+
172
+
173
+ ⇒はい、その通りでございます。具体的にはwebアプリケーションのあるコントローラー内の処理になります。$dataListの初期化は書き忘れておりました。
174
+
175
+
176
+
177
+ 「文字エンコーディングをUTF-8からSJISに変換したあとにimplodeにかけているの、逆じゃないでしょうか。」
178
+
179
+
180
+
181
+ ⇒本当ですね...逆でした。しかしながら、これを逆にして試しましたが結果は変わらずでした...
182
+
183
+
184
+
185
+
186
+
187
+ 他に何か解決方法は考えられますでしょうか。