hrishi
hrishi

Reputation: 1656

Read value within node in XML using PHP xpath

I have XML like below, and I want to read the value of name using PHP XPATH. I can read value if it is between start and end tags, but how to read value if XML is like this?

<a name="john" id="100" />

Upvotes: 0

Views: 28

Answers (1)

Philipp Palmtag
Philipp Palmtag

Reputation: 1328

You can use this to get the value of name:

$xml = new SimpleXMLElement('<a name="john" id="100" />');

$name = (string)$xml->xpath('/a/@name')[0];

echo $name; // Output: john

Upvotes: 1

Related Questions