回答編集履歴

1

参考

2017/05/11 08:51

投稿

yambejp
yambejp

スコア114878

test CHANGED
@@ -55,3 +55,55 @@
55
55
 
56
56
 
57
57
  ```
58
+
59
+
60
+
61
+ # 参考までに
62
+
63
+
64
+
65
+ ```PHP
66
+
67
+ $html=<<<eof
68
+
69
+ <a href="test.htm">test1</a>
70
+
71
+ <a href='/test/test.htm'>test2</a>
72
+
73
+ <a href='../test/test.htm'>test3</a>
74
+
75
+ eof;
76
+
77
+
78
+
79
+ $pattern="/(?<=href=([\"']))(?!https?:\/\/).+?(?=\\1)/mis";
80
+
81
+ preg_match_all($pattern,$html,$match);
82
+
83
+ print_r($match[0]);
84
+
85
+
86
+
87
+ $replacement=function($match){
88
+
89
+ $path =$_SERVER["REQUEST_SCHEME"]."://";
90
+
91
+ $path.=$_SERVER["SERVER_NAME"];
92
+
93
+ if(!preg_match("|^/|",$match[0])){
94
+
95
+ $path.=dirname($_SERVER["REQUEST_URI"])."/";
96
+
97
+ }
98
+
99
+ return $path.$match[0];
100
+
101
+ };
102
+
103
+ $html=preg_replace_callback($pattern,$replacement,$html);
104
+
105
+ print "<pre>";
106
+
107
+ print htmlspecialchars($html);
108
+
109
+ ```