<?php
$html = <<<'EOD'
aaa
<a href="link.php?x=banana">text 1</a>
bbb
<a href="different_url.php">text 2</a>
ccc
<a href="link.php?x=orange">text 3</a>
ddd
EOD;
$dom = new DOMDocument;
$dom->loadHTML('<div id="root">' . $html . '</div>');
$nodeList = $dom->getElementsByTagName('a');
foreach($nodeList as $link) {
$query = parse_url($link->getAttribute('href'), PHP_URL_QUERY);
if ( !$query ) continue;
parse_str($query, $arr);
if ( isset($arr['x']) )
$link->parentNode->insertBefore($dom->createTextNode($arr['x'] . ' '), $link);
}
$result = '';
foreach($dom->getElementById('root')->childNodes as $childNode) {
$result .= $dom->saveHTML($childNode);
}
echo $result;
- 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.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- aaa
banana <a href="link.php?x=banana">text 1</a>
bbb
<a href="different_url.php">text 2</a>
ccc
orange <a href="link.php?x=orange">text 3</a>
ddd
preferences:
132.18 ms | 408 KiB | 5 Q