3v4l.org

run code in 300+ PHP versions simultaneously
<?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);
Output for git.master, git.master_jit, rfc.property-hooks
Array ( [0] => Array ( [0] => Test 1 [1] => (target1) ) [1] => Array ( [0] => Test 2 [1] => (target2) ) )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
35.12 ms | 401 KiB | 8 Q