2014-04-20 53 views
0

parsing这个rssphp,我试图显示在YouTube video.In第一item我得到一个错误,并且不显示视频。在第二个item视频正常显示。
我必须从该行删除123.gr//解析RSS使用PHP,功能的preg_match“编辑:str_replace函数”

<iframe width="780" height="470" src="'http://123.gr///www.youtube.com/embed/WDK5BTuFMRM?rel=0&vq=hd1080" frameborder="0" allowfullscreen></iframe> 

我认为这是一个好主意要做到这一点是使用explode function,但我没有找到正确的路呢。

就拿在我的代码一看:

<?php 
      $html = ""; 
      $url = "123.xml"; 
      $xml = simplexml_load_file($url); 
      for ($i = 0; $i < 10; $i++){ 
       $title = $xml->channel->item[$i]->title; 
       $description = $xml->channel->item[$i]->description; 
       print_r(explode('"',$description,5)); 
?> 
     <div data-role="content"> 
      <div data-role="collapsible" data-theme="b"> 
       <h3><?php echo $title ?></h3> 
       <p><?php echo $description ?></p> 
      </div> 
     </div> 
     <?php 
      } 
     ?> 

这里是RSS

<item> 
    <title>This is the title</title> 
    <description> 
    <![CDATA[Some text 
     <p> 
     <div align="center"> 
     <iframe width="780" height="470" src="'http://123.gr///www.youtube.com/embed/WDK5BTuFMRM?rel=0&vq=hd1080" frameborder="0" allowfullscreen></iframe> 
     </div> 
    ]]> 
    </description> 
</item> 
<item> 
    <title>This is the title</title> 
    <description> 
    <![CDATA[Some text 
     <p> 
     <div align="center"> 
     <iframe width="780" height="470" src="'http://www.youtube.com/embed/WDK5BTuFMRM?rel=0&vq=hd1080" frameborder="0" allowfullscreen></iframe> 
     </div> 
     ]]> 
    </description> 
</item> 

任何帮助将不胜感激

回答

0

Finnally我使用str_replace函数,并同时显示videos.Here的是我的代码,也许会帮助别人,将来

<?php 
     $html = ""; 
     $url = "http://123.gr/index.php?format=feed&type=rss"; 
       $xml = file_get_contents($url); 
      $x = new SimpleXmlElement($xml); 


      foreach ($x->channel->item as $entry){ 
       $part_to_replace = array("http://123.gr///www.youtube.com/"); 
       $replaced = array("http://www.youtube.com/"); 
       $entry->description = str_replace($part_to_replace, $replaced, $entry->description); 
       echo $entry->description; 
        echo $entry->title;  
     ?> 
0

你可以使用这样的事情让所有的样本youtube链接:

$xml = file_get_contents($url); 
preg_match_all('[(?P<link>www\.youtube\.com.*?)"]', $xml, $matches); 
var_dump($matches); 

这会给你一个$matches的数组,其中包含你的rss xml中找到的youtube视频的所有链接。

+0

谢谢您的回答本杰明,但我需要保持iframe元素​​ – marYOs

+0

实际上我希望以src开始:src =“http://youtube.com/ .. ...“ – marYOs

+0

了解它是如何工作的。晚上我会上传代码! – marYOs