Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
function for people whining that preg_match_all is less to type and concluding from it that regex must be better for parsing html than a dom parser
* Run an XPath query against an HTML string and returns the results
* @param string $xpath The XPath query to run on $thml
* @param string $html The HTML to parse. Uses the previously used string if omitted
* @return array
function xpath_match_all($query, $html = '')
static $dom;
static $xpath;
static $content;
if (!$dom) {
$dom = new DOMDocument;
if ($html !== '') {
$content = $html;
$xpath = new DOMXPath($dom);
$matches = array(array(), array());
foreach ($xpath->evaluate($query) as $i => $resultNode) {
$save = version_compare(PHP_VERSION, '5.3.6', '<') ? 'saveXml' : 'saveHtml';
$matches[0][] = $dom->$save($resultNode);
$innerHtml = '';
if ($resultNode->childNodes) {
foreach ($resultNode->childNodes as $childNode) {
$innerHtml .= $dom->$save($childNode);
$matches[1][] = $innerHtml;
return $matches;

Store the current state of $user_errors in libxml_use_internal_errors()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.