I am trying to filter html tables with regex matching their id attribute. What am i doing wrong? Code i am trying to implement:
$this->xpath = new DOMXPath($this->dom);
$this->xpath->registerNamespace("php", "http://php.net/xpath");
$this->xpath->registerPHPFunctions();
foreach($xpath->query("//table[php:function('preg_match', '/post\d+/', @id)]") as $key => $row)
{
}
Error that i get: preg_match expects second param to be a string, array given.
An attribute is still a complex element according to DOM (has a namespace etc.). Use:
Now, we need a boolean return, so:
BTW: for more complex issues, you can of course sneakily check what’s happening with this:
It’s a shame we don’t have XPATH 2.0 functions available, but if you can handle this requirement with a more unreliable
starts-with, I’d always prefer that over importing PHP functions.