Php 如何从Atom提要XML中删除HTML标记
我为基于Laravel的博客准备了一个XML提要。当我使用feedvalidator检查我的提要时。我收到一个关于youtube嵌入视频的错误:Php 如何从Atom提要XML中删除HTML标记,php,regex,xml,laravel,Php,Regex,Xml,Laravel,我为基于Laravel的博客准备了一个XML提要。当我使用feedvalidator检查我的提要时。我收到一个关于youtube嵌入视频的错误: line 24, column 0: content should not contain iframe tag (8 occurrences) [help] 我在控制器中获取提要: public function index() { $data['posts'] = Post::orderBy('created_at', 'DESC
line 24, column 0: content should not contain iframe tag (8 occurrences) [help]
我在控制器中获取提要:
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 ',
]);
}
我的提要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>
{{'}
锡尔坦塔利拉尔·托普卢奥
Üzerinde GüneşBatmayan Topluluk
{{Carbon\Carbon::now()->toATOMString()}
萨尔特安塔尔酒店
tag:sirtcantallar.com,{date('Y-m-d')}:/{{{Carbon\Carbon::now()->toATOMString()}
@foreach($posts作为$post)
{{$post->author->name}
{{$post->title}
{{$post->created_at->toATOMString()}
{{post_tag_uri($post)}
@如果(strlen($post->minicontent)>0)
{{$post->minicontent}
@否则
{Str::words(strip_标签(preg_replace(“/?[a-z0-9]{2,8};/i”,”,“,$post->content)),13)}
@恩迪夫
内容}}]>
内容)}}>
@endforeach
如何从内容中删除iFrame?
编辑1:我添加了此功能:
function rss_noiframe($content) {
$content = preg_replace( '/<iframe(.*)\/iframe>/is', '', $content );
return $content;
}
函数rss\u noiframe($content){
$content=preg_replace('//is',''$content);
返回$content;
}
并尝试获取视图:
<content type="html"><![CDATA[{{rss_noiframe($post->content)}}]]></content>
content)}}]>
只是一个简短的想法,阅读内容标签的CData如何
content type="html"><![CDATA[{{$post->content}}]]></content>
然后将元素放入$ret中
(对不起,我这里没有php工作环境,所以我可以给你一个抽象的工作原理)
我认为这将使您比仅仅使用正则表达式更加安全。对于那些希望从我用以下代码解决的问题中得到答案的人:
<content type="html"><![CDATA[{{preg_replace( '/<iframe(.*)\/iframe>/is', '', $post->content )}}]]></content>
content)}}]>
编辑全文可能会对html结构的有效性造成危险。您应该考虑在DOM结构内部执行此操作,因为它更可靠。如果你真的想使用正则表达式,一定要将它设置为一个不加密模式,好像你得到了1个SomerandomText2可能会杀死“somerandomtext”,但这不是你想要的
<content type="html"><![CDATA[{{preg_replace( '/<iframe(.*)\/iframe>/is', '', $post->content )}}]]></content>