回答編集履歴

4

調整

2019/03/27 01:40

投稿

yambejp
yambejp

スコア114896

test CHANGED
@@ -130,7 +130,7 @@
130
130
 
131
131
  <p>hogehoge</p><p>hogehoge</p>
132
132
 
133
- <p><img src="https://hogehoge.png&quot; alt="hogehoge?" height="123" width="123"></p>
133
+ <p><img src="https://hogehoge.png" alt="hogehoge?" height="123" width="123"></p>
134
134
 
135
135
  <h2>hogehoge</h2>
136
136
 

3

調整

2019/03/27 01:40

投稿

yambejp
yambejp

スコア114896

test CHANGED
@@ -111,3 +111,63 @@
111
111
  print_r($data);
112
112
 
113
113
  ```
114
+
115
+
116
+
117
+ # 提示されたソース分
118
+
119
+ 例示のソースは一部クオートの表示がおかしいですが、それを差し引いても特に問題なさそうです
120
+
121
+ ```PHP
122
+
123
+ <?PHP
124
+
125
+ $str=<<<eof
126
+
127
+ <h2 id="123" class="1234">hogehoge</h2>
128
+
129
+ <p>hogehoge<strong style="color: rgb(0, 0, 255);">hogehoge</strong>hogehoge</p>
130
+
131
+ <p>hogehoge</p><p>hogehoge</p>
132
+
133
+ <p><img src="https://hogehoge.png&quot; alt="hogehoge?" height="123" width="123"></p>
134
+
135
+ <h2>hogehoge</h2>
136
+
137
+ <p>hogehoge</p><p>hogehoge<strong style="color: rgb(0, 0, 255);">hogehoge</strong></p>
138
+
139
+ <p>hogehoge</p>
140
+
141
+ <h2>hogehoge</h2>
142
+
143
+ <p>hogehoge<strong style="color: rgb(0, 0, 255);">hogehoge</strong></p>
144
+
145
+ <p>hogehoge</p><p>hogehoge</p><p>
146
+
147
+ <img src="https://hogehoge.png" alt="hogehoge" height="111" width="222"></p>
148
+
149
+ eof;
150
+
151
+
152
+
153
+ $data=[];
154
+
155
+ $pattern="@<(h[23])(\s\S+?)*>.+?</\1>|<.+?>@mis";
156
+
157
+ while(preg_match($pattern,$str,$matches)){
158
+
159
+ $str= preg_replace($pattern,"%s",$str,1);
160
+
161
+ $data[]=$matches[0];
162
+
163
+ }
164
+
165
+ $str= str_replace("hogehoge","<span>piyopiyo</span>",$str);
166
+
167
+ $str= vsprintf($str,$data);
168
+
169
+ print $str;
170
+
171
+ print_r($data);
172
+
173
+ ```

2

chousei

2019/03/27 01:39

投稿

yambejp
yambejp

スコア114896

test CHANGED
@@ -53,3 +53,61 @@
53
53
  ```
54
54
 
55
55
  ※一部調整
56
+
57
+
58
+
59
+ # test
60
+
61
+
62
+
63
+ h2,h3に属性を含むもの
64
+
65
+ ```PHP
66
+
67
+ <?PHP
68
+
69
+ $str=<<<eof
70
+
71
+ <h2>hogehoge</h2>
72
+
73
+ <h3>hogehoge</h3>
74
+
75
+ <h2 id="123abc">hogehoge</h2>
76
+
77
+ <h3>hogehoge<span>hogehoge</span>hogehoge</h3>
78
+
79
+ <h2>fugafuga</h2>
80
+
81
+ <p>hogehoge<span>hogehoge</span>hogehoge</p>
82
+
83
+ <p><strong>hogehoge</strong></p>
84
+
85
+ <span>hogehoge</span>
86
+
87
+ hogehoge
88
+
89
+ <img src="hogehoge" alt="hogehoge">
90
+
91
+ eof;
92
+
93
+ $data=[];
94
+
95
+ $pattern="@<(h[23])(\s\S+?)*>.+?</\1>|<.+?>@mis";
96
+
97
+ while(preg_match($pattern,$str,$matches)){
98
+
99
+ $str= preg_replace($pattern,"%s",$str,1);
100
+
101
+ $data[]=$matches[0];
102
+
103
+ }
104
+
105
+ $str= str_replace("hogehoge","<span>piyopiyo</span>",$str);
106
+
107
+ $str= vsprintf($str,$data);
108
+
109
+ print $str;
110
+
111
+ print_r($data);
112
+
113
+ ```

1

調整

2019/03/27 00:42

投稿

yambejp
yambejp

スコア114896

test CHANGED
@@ -30,15 +30,13 @@
30
30
 
31
31
  $data=[];
32
32
 
33
- foreach(["|<h2>.+?</h2>|mis","|<h3>.+?</h3>|mis","|<.+?>|mis"] as $pattern){
33
+ $pattern="@<h2>.+?</h2>|<h3>.+?</h3>|<.+?>@mis";
34
34
 
35
- while(preg_match($pattern,$str,$matches)){
35
+ while(preg_match($pattern,$str,$matches)){
36
36
 
37
- $str= preg_replace($pattern,"%s",$str,1);
37
+ $str= preg_replace($pattern,"%s",$str,1);
38
38
 
39
- $data[]=$matches[0];
39
+ $data[]=$matches[0];
40
-
41
- }
42
40
 
43
41
  }
44
42
 
@@ -48,4 +46,10 @@
48
46
 
49
47
  print $str;
50
48
 
49
+ print_r($data);
50
+
51
+
52
+
51
53
  ```
54
+
55
+ ※一部調整