我使用PHP从XML文件中获取一些数据。代码非常简单,类似于:
// load XML file
$xml = simplexml_load_file('http://www.something.com/rss/rss.xml') or die ("Unable to load XML!");
// access XML data
echo "Title for 1 " . $xml->channel->item[0]->title . "<br>";
echo "Link for 1: " . $xml->channel->item[1]->link . "<br>";
echo "Description for 1: " . $xml->channel->item[1]->description . "<br>";
它工作正常,但在这里我发现问题,描述包含大量数据,具体看起来像:
<description>Duration : 6 min<br>Url : http://www.videosite.com/video5261542/name_of_video<br><img src='http://img100-542.link_on_image.jpg'><br><img src='http://img100-542.link_on_another_image.jpg'><br>&lt;div id=&quot;xv-embed-5261542&quot;&gt;&lt;/div&gt; &lt;script type=&quot;text/javascript&quot;&gt; (function() { var tn = document.createElement('script'); tn.type = 'text/javascript'; tn.async = true; tn.src = 'http://flashservice.xvideos.com/embedcode/5261542/510/400/embed.js'; var s = document.getElementById('xv-embed-5261542'); s.parentNode.insertBefore(tn, s); })(); &lt;/script&gt;<br> </description>
只考虑我想从这里开始是DURATION时间和两个图像链接,每个都是separte,我在考虑使用爆炸功能但不确定如何做或者分裂的条件
答案 0 :(得分:-1)
试试这个;)
<?php
// load XML file
$xml = simplexml_load_file('http://www.something.com/rss/rss.xml') or die ("Unable to load XML!");
$desc = $xml->channel->item[1]->description;
preg_match('~Duration : (?<duration>.+)<br>~isU', $desc, $duration);
preg_match_all('~<img src=\'(?<url>[^\']+)\'~isU', $desc, $images);
$duration = $duration['duration'];
$images = $images['url'];
// access XML data
echo "Title for 1 " . $xml->channel->item[0]->title . "<br>";
echo "Link for 1: " . $xml->channel->item[1]->link . "<br>";
echo "Duration for 1: " . $duration . "<br>";
echo "Images for 1: " . implode('<br>', $images) . "<br>";
输出
Title for 1 ---TITLE---
Link for 1: http://www.something.com/xyz
Duration for 1: 22 min
Images for 1: http://www.something.com/xyz.6.jpg
http://www.something.com/xyz-5.6.jpg