Trying to count if a name is present in the XML file and how many times, can anybody help? I'm already on version 35 with internet code I've tried but mostly it only counts the tags instead of the content between the 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);
// to detect, count if variable NameToFind is present
$NameToFind="Alex Pepper";
// Missing code
echo "$NameToFind is x times present in the XML ";
Done some attemps with all your comments and finally this was the working code I needed:
$xml=simplexml_load_file('book.xml');
$nodes= $xml->xpath("//Book/Contact[contains(.,'$NameToFind')]");
$count = count($nodes);