Question

i was trying to extract caption,url and image link from this source and i have used following preg_match and it works but it gives only for one set but i want for all set .for example here it is three set so i want to get details of all three. i know in anchor we can use getattribute('title') alt an etc but how to use here .

 <urlset>

  <url><loc>/1366x768/citroen-ds-cabrio-auto-car-wallshark-com-228615.html</loc><image:image><image:loc>s/1366x768/citroen-ds/228615/citroen-ds-cabrio-auto-car-wallshark-com-228615.jpg</image:loc><image:caption>Citroen Ds Cabrio Auto Car Wallshark Com  Walpapers</image:caption></image:image></url>

 <url><loc>/1366x768/citroen-ds-cars-citro-n-cabrio-213157.html</loc><image:image><image:loc>s/1366x768/citroen-ds/213157/citroen-ds-cars-citro-n-cabrio-213157.jpg</image:loc><image:caption>Citroen Ds Cars Citro N Cabrio  Walpapers</image:caption></image:image></url>

  <url><loc>/1366x768/citroen-ds-citro-n-pictures-95569.html</loc><image:image><image:loc>s/1366x768/citroen-ds/95569/citroen-ds-citro-n-pictures-95569.jpg</image:loc><image:caption>Citroen Ds Citro N Pictures  Walpapers</image:caption></image:image></url>
  </urlset>

here is my preg match for caption and image link which works fine but only for one

            preg_match("/\<image:caption\>(.*)\<\/image:caption\>/",$str,$title);

            preg_match("/\<image:loc\>(.*)\<\/image:loc\>/",$str,$title);

how to make it work to extract all details

Was it helpful?

Solution

Maybe you need preg_match_all instead of preg_match? A better approach:

   preg_match_all("/<image:caption>.*?<\/image:caption>|<image:loc>.*?<\/image:loc>|<loc>.*?<\/loc>/", $text, $results);
   $arr = array_chunk(array_map('strip_tags', $results[0]), 3);
   print_r($arr);

Output:

Array
(
    [0] => Array
        (
            [0] => /1366x768/citroen-ds-cabrio-auto-car-wallshark-com-228615.html
            [1] => s/1366x768/citroen-ds/228615/citroen-ds-cabrio-auto-car-wallshark-com-228615.jpg
            [2] => Citroen Ds Cabrio Auto Car Wallshark Com  Walpapers
        )

    [1] => Array
        (
            [0] => /1366x768/citroen-ds-cars-citro-n-cabrio-213157.html
            [1] => s/1366x768/citroen-ds/213157/citroen-ds-cars-citro-n-cabrio-213157.jpg
            [2] => Citroen Ds Cars Citro N Cabrio  Walpapers
        )

    [2] => Array
        (
            [0] => /1366x768/citroen-ds-citro-n-pictures-95569.html
            [1] => s/1366x768/citroen-ds/95569/citroen-ds-citro-n-pictures-95569.jpg
            [2] => Citroen Ds Citro N Pictures  Walpapers
        )

)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top