回答編集履歴
1
効率化
test
CHANGED
@@ -31,3 +31,43 @@
|
|
31
31
|
ただしクォーテーション内のクォーテーションなど特殊な場合があるので
|
32
32
|
|
33
33
|
完璧にやるには例外処理が必要
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
# 一部修正
|
38
|
+
|
39
|
+
こうした方がより効率的かな
|
40
|
+
|
41
|
+
```PHP
|
42
|
+
|
43
|
+
<?PHP
|
44
|
+
|
45
|
+
$pattern="/(?<=alt=(['\"])).+(?=\\1)/i";
|
46
|
+
|
47
|
+
$replacement="hello world";
|
48
|
+
|
49
|
+
$html=<<<eof
|
50
|
+
|
51
|
+
test
|
52
|
+
|
53
|
+
<img src="hoge.jpg" alt="hoge">
|
54
|
+
|
55
|
+
<img src='fuga.jpg' alt='fuga'>
|
56
|
+
|
57
|
+
<img src='piyo.jpg' Alt='piyo'>
|
58
|
+
|
59
|
+
test
|
60
|
+
|
61
|
+
eof;
|
62
|
+
|
63
|
+
$html = preg_replace($pattern, $replacement, $html );
|
64
|
+
|
65
|
+
print htmlspecialchars($html);
|
66
|
+
|
67
|
+
?>
|
68
|
+
|
69
|
+
```
|
70
|
+
|
71
|
+
ちなみに(?<=)は肯定後読み、(?=)は肯定先読み
|
72
|
+
|
73
|
+
覚えると便利、\\1は事前にヒットした['\"]を再利用しています
|