3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * How to get a json array of the text within a class? * * @link http://stackoverflow.com/a/23543493/367456 */ $html = '<body>... <th class="name" align="left" scope="col"> <a class="foo" href="foo.html">foo</a> </th> ... <th class="name" align="left" scope="col"> <a class="bar" href="bar.html">bar</a> </th> ... <th class="name" align="left" scope="col"> <a class="ba" href="baz.html">baz</a> </th></body>'; $names = function($html) { $doc = new DOMDocument(); $last = libxml_use_internal_errors(TRUE); $doc->loadHTML($html); libxml_use_internal_errors($last); $xp = new DOMXPath($doc); $result = array(); foreach ($xp->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' name ')]") as $node) $result[trim($node->textContent)] = 1; return array_keys($result); }; echo json_encode(array("names" => $names($html)));
Output for git.master, git.master_jit, rfc.property-hooks
{"names":["foo","bar","baz"]}

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:
53.44 ms | 401 KiB | 8 Q