从此字符串中提取详细信息

时间:2014-01-04 20:34:31

标签: php preg-match preg-match-all

我试图从这个源提取标题,网址和图片链接,我使用了以下preg_match并且它可以工作,但它只提供一组但我想要所有设置。例如这里它是三套所以我想要获取所有三个的详细信息。我知道在锚中我们可以使用getattribute(' title')alt等,但是如何在这里使用。

 <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>

这是我的标题和图片链接的匹配,它可以很好地工作,但只适用于一个

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

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

如何使其工作以提取所有细节

1 个答案:

答案 0 :(得分:0)

也许您需要preg_match_all代替preg_match?更好的方法:

   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);

<强>输出

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
        )

)