回答編集履歴

2

修正

2016/09/01 02:02

投稿

yambejp
yambejp

スコア114833

test CHANGED
@@ -36,9 +36,9 @@
36
36
 
37
37
 
38
38
 
39
- #追記
39
+ #修正
40
40
 
41
- もしくはpreg_replace_callback()で置換
41
+ preg_replace()で十分でした
42
42
 
43
43
  ```php
44
44
 
@@ -60,13 +60,9 @@
60
60
 
61
61
  $pattern="/<(title)>(.*?)<\/\\1>/i";
62
62
 
63
- $replacement=function($m){
63
+ $replacement="\\0".PHP_EOL."<h1>\\2</h1>";
64
64
 
65
- return $m[0].PHP_EOL."<h1>".$m[2]."</h1>";
66
-
67
- };
68
-
69
- $html=preg_replace_callback($pattern,$replacement,$html);
65
+ $html=preg_replace($pattern,$replacement,$html);
70
66
 
71
67
  print nl2br(htmlspecialchars($html));
72
68
 

1

追記

2016/09/01 02:02

投稿

yambejp
yambejp

スコア114833

test CHANGED
@@ -33,3 +33,41 @@
33
33
 
34
34
 
35
35
  ```
36
+
37
+
38
+
39
+ #追記
40
+
41
+ もしくはpreg_replace_callback()で置換
42
+
43
+ ```php
44
+
45
+ $html=<<<eof
46
+
47
+ <html>
48
+
49
+ <test>
50
+
51
+ <title>文字列A</title>
52
+
53
+ <test>
54
+
55
+ </html>
56
+
57
+
58
+
59
+ eof;
60
+
61
+ $pattern="/<(title)>(.*?)<\/\\1>/i";
62
+
63
+ $replacement=function($m){
64
+
65
+ return $m[0].PHP_EOL."<h1>".$m[2]."</h1>";
66
+
67
+ };
68
+
69
+ $html=preg_replace_callback($pattern,$replacement,$html);
70
+
71
+ print nl2br(htmlspecialchars($html));
72
+
73
+ ```