teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

6

誤記修正

2016/12/09 14:35

投稿

Y.H.
Y.H.

スコア7918

answer CHANGED
@@ -59,9 +59,9 @@
59
59
  ```
60
60
 
61
61
  ###方法3
62
- - 対象のテキストファイルを全て読み込み(行ごとのarrayに読み込む)
62
+ 0. 対象のテキストファイルを全て読み込み(行ごとのarrayに読み込む)
63
- - 読み込んだarray3番目の要素の前に改行を2つの要素として挿入
63
+ 0. 読み込んだarrayの3番目の要素の前に改行を2つの要素として挿入
64
- - 2の結果を出力
64
+ 0. 2の結果を出力
65
65
 
66
66
  ```PHP
67
67
  <?php

5

コメントを受けて方法3を追加

2016/12/09 14:35

投稿

Y.H.
Y.H.

スコア7918

answer CHANGED
@@ -56,5 +56,17 @@
56
56
  $data = file_get_contents('sample.txt');
57
57
  $out = preg_replace("/^(.*\n.*\n)(.*)/", '${1}' . "\n\n" . '${2}', $data);
58
58
  $data = file_put_contents('sample.txt', $out);
59
+ ```
59
60
 
61
+ ###方法3
62
+ - 対象のテキストファイルを全て読み込み(行ごとのarrayに読み込む)
63
+ - 読み込んだarray3番目の要素の前に改行を2つの要素として挿入
64
+ - 2の結果を出力
65
+
66
+ ```PHP
67
+ <?php
68
+ $filepath = 書き込むファイルの絶対パス
69
+ $ret_array = file($filepath);
70
+ $ret_array = array_splice($ret_array,2,0, array("\n", "\n"));
71
+ file_put_contents($filepath, $ret_array);
60
72
  ```

4

コメントを受けて結果を追加&コードにdisplay_errors,error_reporting追加

2016/12/09 14:26

投稿

Y.H.
Y.H.

スコア7918

answer CHANGED
@@ -8,6 +8,8 @@
8
8
 
9
9
  ```PHP
10
10
  <?php
11
+ ini_set( 'display_errors', 1 );
12
+ ini_set( 'error_reporting', E_ALL );
11
13
  $lines = file('sample.txt');
12
14
  $fp=fopen("sample.txt","w");
13
15
  foreach ($lines as $line_num => $line) {
@@ -18,7 +20,28 @@
18
20
  fputs($fp, $line);
19
21
  }
20
22
  fclose($fp);
23
+ ```
21
24
 
25
+ 結果
26
+ ```PHP
27
+ $ cat sample.txt
28
+ a
29
+ a
30
+ a
31
+ $ od -tx1 sample.txt
32
+ 0000000 61 0a 61 0a 61 0a
33
+ 0000006
34
+ $ php ins2line.php
35
+ $ cat sample.txt
36
+ a
37
+ a
38
+
39
+
40
+ a
41
+ $ od -tx1 sample.txt
42
+ 0000000 61 0a 61 0a 0a 0a 61 0a
43
+ 0000010
44
+ $
22
45
  ```
23
46
 
24
47
  ###方法2
@@ -28,6 +51,8 @@
28
51
 
29
52
  ```PHP
30
53
  <?php
54
+ ini_set( 'display_errors', 1 );
55
+ ini_set( 'error_reporting', E_ALL );
31
56
  $data = file_get_contents('sample.txt');
32
57
  $out = preg_replace("/^(.*\n.*\n)(.*)/", '${1}' . "\n\n" . '${2}', $data);
33
58
  $data = file_put_contents('sample.txt', $out);

3

コードブロックに修正

2016/12/09 06:38

投稿

Y.H.
Y.H.

スコア7918

answer CHANGED
@@ -26,7 +26,10 @@
26
26
  - 読み込んだものを文字列整形
27
27
  - 文字列成形したものを出力
28
28
 
29
+ ```PHP
29
30
  <?php
30
31
  $data = file_get_contents('sample.txt');
31
32
  $out = preg_replace("/^(.*\n.*\n)(.*)/", '${1}' . "\n\n" . '${2}', $data);
32
33
  $data = file_put_contents('sample.txt', $out);
34
+
35
+ ```

2

回答追加

2016/12/08 05:38

投稿

Y.H.
Y.H.

スコア7918

answer CHANGED
@@ -1,5 +1,6 @@
1
1
  いろんなやり方があるとは思いますが、テキストファイル操作なので行を挿入するというより以下のような処理になります。
2
2
 
3
+ ###方法1
3
4
  - 対象のテキストファイルを全て読み込み
4
5
  - 「挿入する位置までの行」を出力
5
6
  - 「挿入する行」を出力
@@ -18,4 +19,14 @@
18
19
  }
19
20
  fclose($fp);
20
21
 
21
- ```
22
+ ```
23
+
24
+ ###方法2
25
+ - 対象のテキストファイルを全て読み込み
26
+ - 読み込んだものを文字列整形
27
+ - 文字列成形したものを出力
28
+
29
+ <?php
30
+ $data = file_get_contents('sample.txt');
31
+ $out = preg_replace("/^(.*\n.*\n)(.*)/", '${1}' . "\n\n" . '${2}', $data);
32
+ $data = file_put_contents('sample.txt', $out);

1

追記

2016/12/08 05:37

投稿

Y.H.
Y.H.

スコア7918

answer CHANGED
@@ -1,3 +1,10 @@
1
+ いろんなやり方があるとは思いますが、テキストファイル操作なので行を挿入するというより以下のような処理になります。
2
+
3
+ - 対象のテキストファイルを全て読み込み
4
+ - 「挿入する位置までの行」を出力
5
+ - 「挿入する行」を出力
6
+ - 「挿入する位置以降の行」を出力
7
+
1
8
  ```PHP
2
9
  <?php
3
10
  $lines = file('sample.txt');