如何从Atom Feed XML中删除HTML标记

时间:2015-01-20 09:50:06

标签: php regex xml laravel

我为基于Laravel的博客准备了一个XML Feed。当我使用feedvalidator检查我的Feed时。我收到有关youtube嵌入式视频的错误:

line 24, column 0: content should not contain iframe tag (8 occurrences) [help]

我在控制器中获取我的Feed:

public function index()
{   

    $data['posts'] = Post::orderBy('created_at', 'DESC')->->where('status',,1)-->limit(20)->get();


    return Response::view('rss',$data, 200, [
        'Content-Type' => 'application/xml; charset=UTF-8 ',
    ]);
}

我的Feed xml视图是:

{{ '<?xml version="1.0" encoding="utf-8" ?>' }}
<feed xmlns="http://www.w3.org/2005/Atom"
    xmlns:media="http://search.yahoo.com/mrss/">
    <link rel="self" type="application/atom+xml" href="http://sirtcantalilar.com/feed" />
    <title>Sirtcantalilar Topluluğu</title>
    <subtitle>Üzerinde Güneş Batmayan Topluluk</subtitle>
    <updated>{{ Carbon\Carbon::now()->toATOMString() }}</updated>
    <author>
        <name>Sırtçantalılar</name>
    </author>
    <id>tag:sirtcantalilar.com,{{date('Y-m-d')}}:/{{ Carbon\Carbon::now()->toATOMString() }}</id>

    @foreach($posts as $post)
        <entry>
            <author>
                <name>{{$post->author->name}}</name>
            </author>
            <title>{{ $post->title }}</title>
            <link rel="alternate" type="text/html" href="{{ URL::route('view-post', $post->slug) }}"/>
            <updated>{{$post->created_at->toATOMString() }}</updated>
            <id>{{ post_tag_uri($post)}}</id>
            @if(strlen($post->minicontent) > 0)
            <summary>{{$post->minicontent }}</summary>
            @else
            <summary>{{ Str::words(strip_tags(preg_replace("/&#?[a-z0-9]{2,8};/i","",$post->content)),13)}}</summary>
            @endif
            <content type="html"><![CDATA[{{$post->content}}]]></content>
             <category term="Blog"/>
              <content type="html"><{{nl2br($post->content)}}></content>
     </entry>
    @endforeach

</feed>

如何从内容中删除iframe? 编辑1:我添加了这个功能:

function rss_noiframe($content) {
    $content = preg_replace( '/<iframe(.*)\/iframe>/is', '', $content );

    return $content;
}

并尝试获取视图:

<content type="html"><![CDATA[{{rss_noiframe($post->content)}}]]></content>

2 个答案:

答案 0 :(得分:0)

只是一个简短的想法,如何阅读您的内容标签的CData

   content type="html"><![CDATA[{{$post->content}}]]></content> 

转换为PHP HTMLSimple之类的HTML解析器。

$ret = $html->find('iframe');

然后将元素放入$ ret。

(对不起,我这里没有php workenviroment,所以我可以给你一个抽象的理论) 我认为这比使用正则表达式更能让您更安全。

答案 1 :(得分:0)

对于那些想要从这个问题得到答案的人,我用以下代码解决了:

<content type="html"><![CDATA[{{preg_replace( '/<iframe(.*)\/iframe>/is', '', $post->content )}}]]></content>
相关问题