I'm trying to use XPath in PHP and I get too many elements. This is my code:
libxml_use_internal_errors(true);
$document = new DOMDocument;
$document->strictErrorChecking = false;
$document->loadHTML($text);
$xpath = new DomXPath($document);
$placeholders = $xpath->query('//div[starts-with(@class, "waf-ph-")]');
print '$placeholders->length: ' . $placeholders->length;
There is only ONE element that matches the query. One. Not a single one more. But here's my output:
$placeholders->length: 7
I'm using loadHTML because I won't have full control over the input when I'm done coding and I can't guarantee standards-compliant XHTML. I do intend to use Tidy, but I'm trying not to rely on it yet. But there is only ONE div that matches the XPath.
Further investigation seems to indicate that it is the same element that has been found seven times.
What's going on?
Edit: the source of the DOM file is an HTML file that somewhere contains the following (this is a dummy address):
<div class="waf-ph-https\:\/\/aserver\.com\/apath\/app\.php5">
<p class="notification">This is to be substituted.</p>
</div>
The string "waf-ph-" is found nowhere else in the file.
Edit:
Trying the following:
foreach ($document->placeholders as $node) print $document->saveXML($node);
returns the text of the above DIV seven times.