使用php解析rss,preg_match函数"编辑:str_replace"

时间:2014-04-20 21:33:28

标签: php parsing rss

parsingrss php并且我试图显示YouTube视频。在第一个item我'我收到错误并且没有显示视频。在第二个item中,视频正常显示 我必须从此行中删除123.gr//

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

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

你可以使用这样的东西获取所有youtube链接:

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

这将为您提供$matches中的数组,其中包含您在rss xml中找到的YouTube视频的所有链接。

答案 1 :(得分:0)

最后我使用str_replace并显示两个视频。这是我的代码,也许将来会帮助某人

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