<?php
$str='<li><a href="test1.php">21.03.2017
<ul>Test1</ul>
</a><p>
<a href="test1"></a>
</p>
</li>
<li><a href="test2.php">21.03.2017
<ul>Text2</ul>
</a><p>
<a href="test2.php"></a>
</p>
</li>';
$dom = new DOMDocument;
@$dom->loadHTML($str);
$aList = $dom->getElementsByTagName('a');
foreach ($aList as $a) {
$output[] = array (
'str' => $a->nodeValue,
'href' => $a->getAttribute('href')
);
}
var_dump($output);
- Output for 8.0.30, 8.1.22 - 8.1.28, 8.2.9 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
- array(4) {
[0]=>
array(2) {
["str"]=>
string(26) "21.03.2017
Test1
"
["href"]=>
string(9) "test1.php"
}
[1]=>
array(2) {
["str"]=>
string(0) ""
["href"]=>
string(5) "test1"
}
[2]=>
array(2) {
["str"]=>
string(26) "21.03.2017
Text2
"
["href"]=>
string(9) "test2.php"
}
[3]=>
array(2) {
["str"]=>
string(0) ""
["href"]=>
string(9) "test2.php"
}
}
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
array(4) {
[0]=>
array(2) {
["str"]=>
string(26) "21.03.2017
Test1
"
["href"]=>
string(9) "test1.php"
}
[1]=>
array(2) {
["str"]=>
string(0) ""
["href"]=>
string(5) "test1"
}
[2]=>
array(2) {
["str"]=>
string(26) "21.03.2017
Text2
"
["href"]=>
string(9) "test2.php"
}
[3]=>
array(2) {
["str"]=>
string(0) ""
["href"]=>
string(9) "test2.php"
}
}
- Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.6 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.29, 8.1.0 - 8.1.21, 8.2.0 - 8.2.8
- array(4) {
[0]=>
array(2) {
["str"]=>
string(21) "21.03.2017
Test1"
["href"]=>
string(9) "test1.php"
}
[1]=>
array(2) {
["str"]=>
string(0) ""
["href"]=>
string(5) "test1"
}
[2]=>
array(2) {
["str"]=>
string(21) "21.03.2017
Text2"
["href"]=>
string(9) "test2.php"
}
[3]=>
array(2) {
["str"]=>
string(0) ""
["href"]=>
string(9) "test2.php"
}
}
preferences:
202.74 ms | 404 KiB | 200 Q