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

回答編集履歴

2

修正

2016/09/01 02:02

投稿

yambejp
yambejp

スコア117965

answer CHANGED
@@ -17,8 +17,8 @@
17
17
 
18
18
  ```
19
19
 
20
- #追記
20
+ #修正
21
- もしくはpreg_replace_callback()で置換
21
+ preg_replace()で十分でした
22
22
  ```php
23
23
  $html=<<<eof
24
24
  <html>
@@ -29,9 +29,7 @@
29
29
 
30
30
  eof;
31
31
  $pattern="/<(title)>(.*?)<\/\\1>/i";
32
- $replacement=function($m){
33
- return $m[0].PHP_EOL."<h1>".$m[2]."</h1>";
32
+ $replacement="\\0".PHP_EOL."<h1>\\2</h1>";
34
- };
35
- $html=preg_replace_callback($pattern,$replacement,$html);
33
+ $html=preg_replace($pattern,$replacement,$html);
36
34
  print nl2br(htmlspecialchars($html));
37
35
  ```

1

追記

2016/09/01 02:02

投稿

yambejp
yambejp

スコア117965

answer CHANGED
@@ -15,4 +15,23 @@
15
15
  }
16
16
  print nl2br(htmlspecialchars($html));
17
17
 
18
+ ```
19
+
20
+ #追記
21
+ もしくはpreg_replace_callback()で置換
22
+ ```php
23
+ $html=<<<eof
24
+ <html>
25
+ <test>
26
+ <title>文字列A</title>
27
+ <test>
28
+ </html>
29
+
30
+ eof;
31
+ $pattern="/<(title)>(.*?)<\/\\1>/i";
32
+ $replacement=function($m){
33
+ return $m[0].PHP_EOL."<h1>".$m[2]."</h1>";
34
+ };
35
+ $html=preg_replace_callback($pattern,$replacement,$html);
36
+ print nl2br(htmlspecialchars($html));
18
37
  ```