0

I have a small string in XML format:

$xml = simplexml_load_string('
    <imgs>
        <img mainfoto="true" date_changed="2019-04-07 23:27:50">
            https://example.com/data/i/pictures/size/333_1.jpg
        </img>
        <img date_changed="2020-04-01 12:27:50">
            https://example.com/data/i/pictures/size/333_2.jpg
        </img>
        <img date_changed="2020-04-04 12:27:50">
            https://example.com/data/i/pictures/size/333_3.jpg
        </img>
        <img date_changed="2020-04-07 12:27:50">
            https://example.com/data/i/pictures/size/333_4.jpg
        </img>
    </imgs>
');

I am trying to get the text(url) from between the img tags:

foreach($xml->img as $url){
    var_dump($url);
}

But this returns objects instead of a string(the actual url):

object(SimpleXMLElement)#7 (2) {
  ["@attributes"]=>
  array(2) {
    ["mainfoto"]=>
    string(4) "true"
    ["date_changed"]=>
    string(19) "2019-04-07 23:27:50"
  }
  [0]=>
  string(52) "
https://example.com/data/i/pictures/size/333_1.jpg
"
}
object(SimpleXMLElement)#8 (2) {
  ["@attributes"]=>
  array(1) {
    ["date_changed"]=>
    string(19) "2020-04-01 12:27:50"
  }
  [0]=>
  string(52) "
https://example.com/data/i/pictures/size/333_2.jpg
"
}
object(SimpleXMLElement)#7 (2) {
  ["@attributes"]=>
  array(1) {
    ["date_changed"]=>
    string(19) "2020-04-04 12:27:50"
  }
  [0]=>
  string(52) "
https://example.com/data/i/pictures/size/333_3.jpg
"
}
object(SimpleXMLElement)#8 (2) {
  ["@attributes"]=>
  array(1) {
    ["date_changed"]=>
    string(19) "2020-04-07 12:27:50"
  }
  [0]=>
  string(52) "
https://example.com/data/i/pictures/size/333_4.jpg
"
}

I've also tried using $url[0] but i'm getting some empty objects.

How can i get just the url out as a string?

Melody
  • 127
  • 10

0 Answers0