php parse xml use exactly attribute -


input xml:

 $url = '<?xml version="1.0" encoding="utf-8"?> <all_emp> <emp_detail> <emp emp_name="john"><img>john_1.jpg</img></emp> <emp emp_name="john"><img>john_2.jpg</img></emp> <emp emp_name="john"><img>john_3.jpg</img></emp> <emp emp_name="jo"><img>jo_1.jpg</img></emp> <emp emp_name="jo"><img>jo_2.jpg</img></emp> <emp emp_name="david"><img>david_1.jpg</img></emp> </emp_detail> </all_emp>';  $xml = simplexml_load_string($url) or die("error: cannot create object"); $imgstring =''; foreach ($xml->emp_detail->emp $node ) {          if (strpos((string) $node->attributes()->emp_name, "jo") !== false) {         $imgstring .= (string) $node->img . "<br />";     } } echo ($imgstring); 

current result:

john_1.jpg john_2.jpg john_3.jpg jo_1.jpg jo_2.jpg 

but want img attribute "jo" like,

expected:

jo_1.jpg jo_2.jpg 

how it?

you doing 1 thing wrong here strpos check position of substring , jo substring of john that's why getting values instead can simple use == comparison.

try code snippet here

change to:

if (strpos((string) $node->attributes()->emp_name, "jo") !== false) { 

this:

if ($node->attributes()->emp_name== "jo") { 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -