回答編集履歴
1
効率化
answer
CHANGED
@@ -14,4 +14,24 @@
|
|
14
14
|
print htmlspecialchars($html);
|
15
15
|
```
|
16
16
|
ただしクォーテーション内のクォーテーションなど特殊な場合があるので
|
17
|
-
完璧にやるには例外処理が必要
|
17
|
+
完璧にやるには例外処理が必要
|
18
|
+
|
19
|
+
# 一部修正
|
20
|
+
こうした方がより効率的かな
|
21
|
+
```PHP
|
22
|
+
<?PHP
|
23
|
+
$pattern="/(?<=alt=(['\"])).+(?=\\1)/i";
|
24
|
+
$replacement="hello world";
|
25
|
+
$html=<<<eof
|
26
|
+
test
|
27
|
+
<img src="hoge.jpg" alt="hoge">
|
28
|
+
<img src='fuga.jpg' alt='fuga'>
|
29
|
+
<img src='piyo.jpg' Alt='piyo'>
|
30
|
+
test
|
31
|
+
eof;
|
32
|
+
$html = preg_replace($pattern, $replacement, $html );
|
33
|
+
print htmlspecialchars($html);
|
34
|
+
?>
|
35
|
+
```
|
36
|
+
ちなみに(?<=)は肯定後読み、(?=)は肯定先読み
|
37
|
+
覚えると便利、\\1は事前にヒットした['\"]を再利用しています
|