I am trying to make a long list of xpath query tests into a much more condensed function and having issues getting the xpath queries to work once pulled from mysql.
This is driving me batty, can anyone explain where I am going wrong please?
Example MySQL table:
CREATE TABLE `spidey_regex` (
`id` int(12) NOT NULL auto_increment,
`regexp` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM;
INSERT INTO `spidey_regex` (`id`, `regexp`) VALUES (1, '''//div[starts-with(@id, "stuff")]''');
Example PHP code using xpath:
<?php
$html = '<div id="stuff123"><a href="link1.html">Link 1</a><a href="#link">Link 2</a><a href="link2.html">Link 3</a></div>';
$cnt=0;
$dom = new DOMDocument();
@$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$xpath = new DOMXPath($dom);
$myresult = mysql_query("select * from spidey_regex",$db);
while($myrow = mysql_fetch_array($myresult)){
$regexp = stripslashes(trim($myrow[regexp]));
$result = $xpath->query($regexp); // use xpath with query from MySQL
foreach($result as $e){ $cnt++; }
if($cnt == '0'){
continue;
}
else {
echo "1. Got here!!";
}
}
?>
The double-quote-to-escape thing is a bit weird. If we were to use the normal backslashes, instead we’d see:
In other words, your XPath query has one too many layers of quotes, and is being rejected by the XPath parser. Once you remove the quotes from the value in the database, your query should work.
Proof at the PHP Interactive prompt: