回答編集履歴
11
さらに修正
answer
CHANGED
@@ -57,14 +57,14 @@
|
|
57
57
|
if ($textnode = $node->nextSibling and $textnode->nodeType === XML_TEXT_NODE) {
|
58
58
|
$node->parentNode->removeChild($textnode);
|
59
59
|
} else {
|
60
|
-
$textnode = $dom->createTextNode('
|
60
|
+
$textnode = $dom->createTextNode('');
|
61
61
|
}
|
62
62
|
|
63
63
|
$figure = $dom->createElement('figure');
|
64
64
|
$node->parentNode->replaceChild($figure, $node);
|
65
65
|
$figure->appendChild($node);
|
66
66
|
|
67
|
-
$figcaption = $dom->createElement('figcaption', trim($textnode->nodeValue));
|
67
|
+
$figcaption = $dom->createElement('figcaption', trim($textnode->nodeValue) ?: 'タイトル無し');
|
68
68
|
$figure->appendChild($figcaption);
|
69
69
|
|
70
70
|
}
|
10
中身が無かったりした場合への例外対応
answer
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
EOD;
|
11
11
|
|
12
12
|
echo preg_replace_callback(
|
13
|
-
'@<img( class="aa bb cc etc".*?) />\s*+(\
|
13
|
+
'@<img( class="aa bb cc etc".*?) />\s*+([^\s<]*+)@s',
|
14
14
|
function ($m) {
|
15
15
|
$m[1] = preg_replace('@
|
16
16
|
\s*+(?:
|
@@ -54,8 +54,11 @@
|
|
54
54
|
$node->removeAttribute($attr->name);
|
55
55
|
}
|
56
56
|
|
57
|
-
$textnode = $node->nextSibling
|
57
|
+
if ($textnode = $node->nextSibling and $textnode->nodeType === XML_TEXT_NODE) {
|
58
|
-
|
58
|
+
$node->parentNode->removeChild($textnode);
|
59
|
+
} else {
|
60
|
+
$textnode = $dom->createTextNode('タイトル無し');
|
61
|
+
}
|
59
62
|
|
60
63
|
$figure = $dom->createElement('figure');
|
61
64
|
$node->parentNode->replaceChild($figure, $node);
|
9
修正
answer
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
EOD;
|
11
11
|
|
12
12
|
echo preg_replace_callback(
|
13
|
-
'@<img( class="aa bb cc etc".*?) />(\S*+)@s',
|
13
|
+
'@<img( class="aa bb cc etc".*?) />\s*+(\S*+)@s',
|
14
14
|
function ($m) {
|
15
15
|
$m[1] = preg_replace('@
|
16
16
|
\s*+(?:
|
8
欠点
answer
CHANGED
@@ -25,6 +25,8 @@
|
|
25
25
|
|
26
26
|
##### DOM版 (細かい差異に依存しないので変更により強いです)
|
27
27
|
|
28
|
+
(ただしインデントが崩れたり一部文字がHTMLエンティティに置換されてしまうなど,少し弊害があります…)
|
29
|
+
|
28
30
|
```php
|
29
31
|
<?php
|
30
32
|
|
7
m1
answer
CHANGED
@@ -12,12 +12,12 @@
|
|
12
12
|
echo preg_replace_callback(
|
13
13
|
'@<img( class="aa bb cc etc".*?) />(\S*+)@s',
|
14
14
|
function ($m) {
|
15
|
-
$
|
15
|
+
$m[1] = preg_replace('@
|
16
16
|
\s*+(?:
|
17
17
|
(?:src|alt)(*SKIP)(*FAIL)|[\w-]++
|
18
18
|
)=(?:"[^"]*+"|\'[^\']*+\')
|
19
19
|
@x', '', $m[1]);
|
20
|
-
return "<figure><img$
|
20
|
+
return "<figure><img$m[1]><figcaption>$m[2]</figcaption></figure>";
|
21
21
|
},
|
22
22
|
$content
|
23
23
|
);
|
6
正規表現
answer
CHANGED
@@ -1,10 +1,35 @@
|
|
1
|
-
|
1
|
+
#### 正規表現版 (変更に弱いですが,決め打ちであればこれで十分です)
|
2
2
|
|
3
3
|
```php
|
4
4
|
<?php
|
5
5
|
|
6
|
+
$content = <<<EOD
|
7
|
+
<img class="aa bb cc etc" src="https://hogehoeg.com/○○○.jpg" alt="○○○" width="○○○" height="○○○" />出典:https://△△△.com
|
8
|
+
<img class="aa bb cc etc" src="https://hogehoeg.com/○○○.jpg" alt='xx oo' width="○○○" height="○○○" />出典:https://△△△.com
|
9
|
+
<img class="aa bb cc etc" src="https://hogehoeg.com/○○○.jpg" width="○○○" height="○○○" />出典:https://△△△.com
|
10
|
+
EOD;
|
11
|
+
|
12
|
+
echo preg_replace_callback(
|
13
|
+
'@<img( class="aa bb cc etc".*?) />(\S*+)@s',
|
14
|
+
function ($m) {
|
15
|
+
$f = preg_replace('@
|
16
|
+
\s*+(?:
|
17
|
+
(?:src|alt)(*SKIP)(*FAIL)|[\w-]++
|
18
|
+
)=(?:"[^"]*+"|\'[^\']*+\')
|
19
|
+
@x', '', $m[1]);
|
20
|
+
return "<figure><img$f><figcaption>$m[2]</figcaption></figure>";
|
21
|
+
},
|
22
|
+
$content
|
23
|
+
);
|
24
|
+
```
|
25
|
+
|
26
|
+
##### DOM版 (細かい差異に依存しないので変更により強いです)
|
27
|
+
|
28
|
+
```php
|
29
|
+
<?php
|
30
|
+
|
6
31
|
// 何かこれらを括っている親要素があると仮定 (もしなければ付加してください)
|
7
|
-
$content=<<<EOD
|
32
|
+
$content = <<<EOD
|
8
33
|
<root>
|
9
34
|
<img class="aa bb cc etc" src="https://hogehoeg.com/○○○.jpg" alt="○○○" width="○○○" height="○○○" />出典:https://△△△.com
|
10
35
|
<img class="aa bb cc etc" src="https://hogehoeg.com/○○○.jpg" alt='xx oo' width="○○○" height="○○○" />出典:https://△△△.com
|
5
補足
answer
CHANGED
@@ -39,6 +39,9 @@
|
|
39
39
|
|
40
40
|
}
|
41
41
|
|
42
|
+
$result = $dom->saveXML($dom->documentElement);
|
43
|
+
echo $result;
|
44
|
+
|
42
|
-
// もし最初に親要素を付加したならば,
|
45
|
+
// もし最初に親要素を付加したならば,後からそれを除外してください
|
43
|
-
|
46
|
+
// $result = substr($dom->saveXML($dom->documentElement), 6, -7);
|
44
47
|
```
|
4
内部エラーを使用
answer
CHANGED
@@ -13,10 +13,12 @@
|
|
13
13
|
EOD;
|
14
14
|
|
15
15
|
$dom = new DOMDocument;
|
16
|
+
libxml_use_internal_errors(true);
|
16
|
-
|
17
|
+
$dom->loadHTML(
|
17
18
|
mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'),
|
18
19
|
LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD
|
19
20
|
);
|
21
|
+
libxml_clear_errors();
|
20
22
|
$xpath = new DOMXPath($dom);
|
21
23
|
|
22
24
|
foreach ($xpath->query('//img[@class="aa bb cc etc"]') as $i => $node) {
|
3
トリミング
answer
CHANGED
@@ -32,7 +32,7 @@
|
|
32
32
|
$node->parentNode->replaceChild($figure, $node);
|
33
33
|
$figure->appendChild($node);
|
34
34
|
|
35
|
-
$figcaption = $dom->createElement('figcaption', $textnode->nodeValue);
|
35
|
+
$figcaption = $dom->createElement('figcaption', trim($textnode->nodeValue));
|
36
36
|
$figure->appendChild($figcaption);
|
37
37
|
|
38
38
|
}
|
2
つけ忘れ
answer
CHANGED
@@ -21,7 +21,7 @@
|
|
21
21
|
|
22
22
|
foreach ($xpath->query('//img[@class="aa bb cc etc"]') as $i => $node) {
|
23
23
|
|
24
|
-
foreach ($xpath->query('./@*[not(name()="src")][not(name()="alt")]') as $attr) {
|
24
|
+
foreach ($xpath->query('./@*[not(name()="src")][not(name()="alt")]', $node) as $attr) {
|
25
25
|
$node->removeAttribute($attr->name);
|
26
26
|
}
|
27
27
|
|
1
訂正
answer
CHANGED
@@ -3,26 +3,40 @@
|
|
3
3
|
```php
|
4
4
|
<?php
|
5
5
|
|
6
|
+
// 何かこれらを括っている親要素があると仮定 (もしなければ付加してください)
|
6
7
|
$content=<<<EOD
|
8
|
+
<root>
|
7
|
-
<img class="aa bb cc etc" src="https://hogehoeg.com/○○○.jpg" alt="○○○" width="○○○" height="○○○" />出典:https://△△△.com
|
9
|
+
<img class="aa bb cc etc" src="https://hogehoeg.com/○○○.jpg" alt="○○○" width="○○○" height="○○○" />出典:https://△△△.com
|
8
|
-
<img class="aa bb cc etc" src="https://hogehoeg.com/○○○.jpg" alt='xx oo' width="○○○" height="○○○" />出典:https://△△△.com
|
10
|
+
<img class="aa bb cc etc" src="https://hogehoeg.com/○○○.jpg" alt='xx oo' width="○○○" height="○○○" />出典:https://△△△.com
|
9
|
-
<img class="aa bb cc etc" src="https://hogehoeg.com/○○○.jpg" width="○○○" height="○○○" />出典:https://△△△.com
|
11
|
+
<img class="aa bb cc etc" src="https://hogehoeg.com/○○○.jpg" width="○○○" height="○○○" />出典:https://△△△.com
|
12
|
+
</root>
|
10
13
|
EOD;
|
11
14
|
|
12
15
|
$dom = new DOMDocument;
|
13
16
|
@$dom->loadHTML(
|
14
|
-
mb_convert_encoding(
|
17
|
+
mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'),
|
15
18
|
LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD
|
16
19
|
);
|
17
20
|
$xpath = new DOMXPath($dom);
|
21
|
+
|
18
|
-
foreach ($xpath->query('//img[@class="aa bb cc etc"]') as $node) {
|
22
|
+
foreach ($xpath->query('//img[@class="aa bb cc etc"]') as $i => $node) {
|
23
|
+
|
19
24
|
foreach ($xpath->query('./@*[not(name()="src")][not(name()="alt")]') as $attr) {
|
20
25
|
$node->removeAttribute($attr->name);
|
21
26
|
}
|
27
|
+
|
28
|
+
$textnode = $node->nextSibling;
|
29
|
+
$node->parentNode->removeChild($textnode);
|
30
|
+
|
22
31
|
$figure = $dom->createElement('figure');
|
23
32
|
$node->parentNode->replaceChild($figure, $node);
|
24
33
|
$figure->appendChild($node);
|
34
|
+
|
35
|
+
$figcaption = $dom->createElement('figcaption', $textnode->nodeValue);
|
36
|
+
$figure->appendChild($figcaption);
|
37
|
+
|
25
38
|
}
|
26
39
|
|
40
|
+
// もし最初に親要素を付加したならば,substrで除外してください
|
27
|
-
echo
|
41
|
+
echo $dom->saveXML($dom->documentElement);
|
28
|
-
```
|
42
|
+
```
|