PHP:过滤除脚本和图片以外的所有代码?

时间:2022-11-11 22:25:39

Lets say a member left a comment.

假设一个成员留下了评论。

Hi! Look at these cars.
<img src="http://www.mysite.com/possiblefolder/possiblesub/image.jpg"></img>
<img src="http://othersite.com/possiblefolder/possiblesub/image.jpg"></img>
<img src="http://www.mysite.otherside.com/possiblefolder/image.jpg"></img>
Which is your favorite?

I want the results to come up as:

我希望结果是:

Hi! Look at these cars.
<img src="http://www.mysite.com/possiblefolder/possiblesub/image.jpg"></img>
http://othersite.com/possiblefolder/possiblesub/image.jpg  
http://www.mysite.otherside.com/possiblefolder/possiblesub/image.jpg  
Which is your favorite?

I want to filter all codes except images and scripts coming from my site. Anyone got any ideas?

我想要过滤除来自我的站点的图像和脚本之外的所有代码。任何人有任何想法吗?

5 个解决方案

#1


1  

Hope this helps

希望这有助于

<(\w+).+src=[\x22|'](?![^\x22']+mysite\.com[^\x22']+)([^\x22']+)[\x22|'].*>(?:</\1>)?

Group 1 is the tag used and group 2 is the "src" value so you can do a replace.

第1组是使用的标记,第2组是“src”值,因此可以进行替换。

In Browser Demo

在浏览器的演示

PHP:过滤除脚本和图片以外的所有代码?

#2


1  

In most reasonable cases and in particular in your examples, this will work:

在大多数合理的情况下,特别是在你的例子中,这将会起作用:

$new_comment = preg_replace('%<img.*?\ssrc="(http://(?!www.mysite.com).*?)".*?>.*?</img>%', '\1', $old_comment);

It will give the result you describe.

它会给出你描述的结果。

#3


1  

If it's not proper XHTML, run it through Tidy. If it's already clean XHTML, skip this part

如果它不是合适的XHTML,请运行Tidy。如果已经清除了XHTML,则跳过这一部分

$config = array('output-xhtml'   => true);
$tidy = new tidy();
$html = $tidy->repareString($html, $config, 'utf8');

Now, having clean XHTML you can use XPath:

现在,有了干净的XHTML,您可以使用XPath:

$xhtml = new SimpleXMLElement($html);
foreach ($xhtml->xpath('//*/img') as $img_parent) {
   if(!(strpos($img_parent->img->src, 'http://www.mysite.com/') === 0)) {
     $img_parent->img = new SimpleXMLElement($img_parent->img->src);
   }
}
$cleaned_html = $xhtml->asXML();

#4


0  

You can use PHP strip_tags() to strip all HTML tags out from user-comment (highly recommended), also you need to implement some script code such as BBCode on PHPbb forums, etc...

您可以使用PHP strip_tags()从用户评论(强烈推荐)中删除所有HTML标记,还需要实现一些脚本代码,比如PHPbb论坛上的BBCode等等……

[img]possibleimgdir/someimage.jpg[/img]

later search for [img] and [/img], append your root URL in front of content found between tags (example. http://www.mysite.com/possibleimgdir/someimage.jpg), check if file exists and then create HTML IMG tags for that SRC property if it is valid...

稍后搜索[img]和[/img],在标签之间的内容前面添加根URL(示例)。请检查文件是否存在,然后为该SRC属性创建HTML IMG标记,如果它是有效的……

That's just one of possible ideas!

这只是一种可能的想法!

#5


0  

You could do it with a jQuery oneliner:

你可以用jQuery oneliner:

$('img:not(src^="http://www.mysite.com/")').hide()

#1


1  

Hope this helps

希望这有助于

<(\w+).+src=[\x22|'](?![^\x22']+mysite\.com[^\x22']+)([^\x22']+)[\x22|'].*>(?:</\1>)?

Group 1 is the tag used and group 2 is the "src" value so you can do a replace.

第1组是使用的标记,第2组是“src”值,因此可以进行替换。

In Browser Demo

在浏览器的演示

PHP:过滤除脚本和图片以外的所有代码?

#2


1  

In most reasonable cases and in particular in your examples, this will work:

在大多数合理的情况下,特别是在你的例子中,这将会起作用:

$new_comment = preg_replace('%<img.*?\ssrc="(http://(?!www.mysite.com).*?)".*?>.*?</img>%', '\1', $old_comment);

It will give the result you describe.

它会给出你描述的结果。

#3


1  

If it's not proper XHTML, run it through Tidy. If it's already clean XHTML, skip this part

如果它不是合适的XHTML,请运行Tidy。如果已经清除了XHTML,则跳过这一部分

$config = array('output-xhtml'   => true);
$tidy = new tidy();
$html = $tidy->repareString($html, $config, 'utf8');

Now, having clean XHTML you can use XPath:

现在,有了干净的XHTML,您可以使用XPath:

$xhtml = new SimpleXMLElement($html);
foreach ($xhtml->xpath('//*/img') as $img_parent) {
   if(!(strpos($img_parent->img->src, 'http://www.mysite.com/') === 0)) {
     $img_parent->img = new SimpleXMLElement($img_parent->img->src);
   }
}
$cleaned_html = $xhtml->asXML();

#4


0  

You can use PHP strip_tags() to strip all HTML tags out from user-comment (highly recommended), also you need to implement some script code such as BBCode on PHPbb forums, etc...

您可以使用PHP strip_tags()从用户评论(强烈推荐)中删除所有HTML标记,还需要实现一些脚本代码,比如PHPbb论坛上的BBCode等等……

[img]possibleimgdir/someimage.jpg[/img]

later search for [img] and [/img], append your root URL in front of content found between tags (example. http://www.mysite.com/possibleimgdir/someimage.jpg), check if file exists and then create HTML IMG tags for that SRC property if it is valid...

稍后搜索[img]和[/img],在标签之间的内容前面添加根URL(示例)。请检查文件是否存在,然后为该SRC属性创建HTML IMG标记,如果它是有效的……

That's just one of possible ideas!

这只是一种可能的想法!

#5


0  

You could do it with a jQuery oneliner:

你可以用jQuery oneliner:

$('img:not(src^="http://www.mysite.com/")').hide()