php - Detect if value in XML is present -


this question has answer here:

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

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -