0

i get some data from XML file using PHP. Code is pretty simple, its something like:

// 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>"; 

It works fine but here i found problem, Description contains lots of data, to be specific it looks like:

<description>Duration : 6 min&lt;br&gt;Url : http://www.videosite.com/video5261542/name_of_video&lt;br&gt;&lt;img src='http://img100-542.link_on_image.jpg'&gt;&lt;br&gt;&lt;img src='http://img100-542.link_on_another_image.jpg'&gt;&lt;br&gt;&amp;lt;div id=&amp;quot;xv-embed-5261542&amp;quot;&amp;gt;&amp;lt;/div&amp;gt; &amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;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); })(); &amp;lt;/script&amp;gt;&lt;br&gt; </description>

Only think i want from here is DURATION time and both image links, each of it separte, i was thinkin about using explode function but not sure how to do it or with what condition for split

1 Answers1

-1

Try this ;)

<?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>";

Output

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
Zemistr
  • 1,049
  • 7
  • 10
  • It actualy work mate, just didnt remove URL and i will be really thankfull if it can split each part to different varabile for saving in databse – user3809565 Jul 06 '14 at 13:28