doushi1960 2012-08-22 13:11
浏览 26
已采纳

正则表达式从RSS文件中删除标签和内容

This is the example structure of my RSS file:

<item>
 <title>My Title</title>
 <link>http://www.link.com</link>
 <description>The description</description>
 <author>Blah Blah</author>
 <pubDate>Thu, 26 Jul 2012 10:17:15 -0400</pubDate>
 <media:content url="myimage.jpg">
  <media:title>sdafsd</media:title>
 </media:content>
 <position>1</position>
</item>

How can I remove the author tag and its contents, the entire media:content tag and its contents, and the position tag and its contents completely from the file using PHP regular expressions?

Thanks!

  • 写回答

4条回答 默认 最新

  • donglinli2027 2012-08-22 13:40
    关注

    My previous answer was - rightfully - removed, I should have added it as a comment. Here's an alternative with DomDocument doing exactly what you want to do:

    <?php
    
    $xml = <<<XML
    <?xml version="1.0" encoding="UTF-8"?>
    <rss version="2.0">
      <channel>
        <title>bla</title>
        <link>bla</link>
        <description>A description</description>
        <language>en-us</language>
        <item xmlns:media="http://search.yahoo.com/mrss/">
         <title>My Title</title>
         <link>http://www.link.com</link>
         <description>The description</description>
         <author>Blah Blah</author>
         <pubDate>Thu, 26 Jul 2012 10:17:15 -0400</pubDate>
         <media:content url="myimage.jpg">
          <media:title>sdafsd</media:title>
         </media:content>
         <position>1</position>
        </item>
      </channel>
    </rss>
    XML;
    
    $doc = new DOMDocument();
    $doc->loadXml( $xml );
    
    foreach( $doc->getElementsByTagName( 'item' ) as $item ) {
        $item->removeChild( $item->getElementsByTagName( 'author' )->item( 0 ) );
        $item->removeChild( $item->getElementsByTagName( 'position' )->item( 0 ) );
                $item->removeChild( $item->getElementsByTagName( 'content' )->item( 0 ) );
    }
    
    var_dump( $doc->saveXml( ) );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题