php - Detect if value in XML is present -
this question has answer here:
- searching xml items php xpath 1 answer
trying count if name present in xml file , how many times, can help? i'm on version 35 internet code i've tried counts tags instead of content between tags.
<?php $xml = <<< xml <book> <contact> <name>an smith</name> </contact> <contact> <name>alex pepper</name> </contact> <contact> <name>tom james</name> </contact> ; </book> xml; $dom = new domdocument; $dom->loadxml($xml); // detect, count if variable nametofind present $nametofind="alex pepper"; // missing code echo "$nametofind x times present in xml ";
done attemps comments , working code needed:
$xml=simplexml_load_file('book.xml'); $nodes= $xml->xpath("//book/contact[contains(.,'$nametofind')]"); $count = count($nodes);
probably this:
$xpath = new domxpath($dom); $query = '//book/contact/name[. = "' . $nametofind . '"]'; $entries = $xpath->query($query); $count = count($entries);
although xpath query may little off.
Comments
Post a Comment