How to get values from XML?

Hello!

There is such a repeating XML structure:

<entry>
<author>
<name>Dunnock_D</name>
<uri>http://www.flickr.com/people/dunnock_d/</uri>
</author>
 <link rel="license" type="text/html" href="https://creativecommons.org/licenses/by-nc/2.0/deed.en">
 <link rel="enclosure" type="image/jpeg" href="http://farm8.staticflickr.com/7548/26820724620_1d221c3187_b.jpg">
</entry>


I need to get the value of href only where rel="enclosure" (to link to an image).

Doing this in a loop:

$xml = simplexml_load_string($result);

 foreach ($xml->entry as $pixinfo) {

 echo $pixinfo->link[1]['href'];

 }


But in this structure, then two, then one value link. And when you take via the array index, sometimes you get what you need.
That is, I need to just select only the link rel="enclosure"

Is there any concise decision or need to run additional checks for this in a loop?
The documentation I read didn't help.

Thanks in advance for the help!
July 9th 19 at 12:48
2 answers
July 9th 19 at 12:50
Solution
I'm not really php, but I think something like this:
$xml->xpath("//link[@rel='enclosure']");
July 9th 19 at 12:52
in a pinch you can use preg_match_all
this is a very extreme method! must be some "native" the idea - Bethel_Steuber87 commented on July 9th 19 at 12:55

Find more questions by tags PHPXML