- array_map: documentation ( source)
- print_r: documentation ( source)
- implode: documentation ( source)
- trim: documentation ( source)
<?php
$html = '<p>random</p>
<a href="">Test 1</a> (target1)
<br>
<a href="">Test 2</a> (target2)
<br>
<a href="">Test 3</a> (skip)
// etc
';
$array = array(
'(target1)',
'(target2)'
);
$contains = implode(" or ", array_map(function($x) {
return "contains(., '$x')";
}, $array));
$doc = new DOMDocument();
$doc->loadHTML('<?xml encoding="utf-8" ?>' . $html);
$xpath = new DOMXPath($doc);
$elements = $xpath->query("//a/following-sibling::text()[$contains]");
$results = [];
foreach ($elements as $element) {
$results[] = [$element->previousSibling->nodeValue, trim($element->nodeValue)];
}
print_r($results);