Php 如何替换图像中不需要的内容
在我的评论框中,人们变得很狡猾,他们使用样式来修复评论框外的图像。例如:Php 如何替换图像中不需要的内容,php,Php,在我的评论框中,人们变得很狡猾,他们使用样式来修复评论框外的图像。例如: Hellow how are you? <br> <img src="http://example.com/image/gifts/19.png" style="position: absolute; left: 150px; top: 150px;"> 我想从中删除此样式部分 我试着这样做,它也能工作,但如果我继续用大写和小写字母书写,时间会很长 PHP: function unwant($te
Hellow how are you? <br>
<img src="http://example.com/image/gifts/19.png" style="position: absolute; left: 150px; top: 150px;">
我想从中删除此样式部分
我试着这样做,它也能工作,但如果我继续用大写和小写字母书写,时间会很长
PHP:
function unwant($text)
{
$unwant = array(
'style' => '',
'Style' => '',
'STYle' => '',
'STYLe' => '',
'STYLE' => ''
);
return str_replace(array_keys($unwant), array_values($unwant), $text);
}
我想删除它:style=“position:absolute;left:150px;top:150px;”
有更好的方法吗
2。还有一件事
如果用户仅发布图像URL
http://example.com/image/gifts/19.png
我想把它换成
<img src="http://example.com/image/gifts/19.png">
对于第1点,您可以使用str\u replace或preg\u match等删除内容,也可以解析DOM并以这种方式处理节点
<?php
$doc = new DOMDocument();
$doc->loadHTML($html);
$images = $doc->getElementsByTagName('img');
foreach ($images as $image) {
$image->removeAttributeNode('style');
}
$cleanHtml = $doc->saveHtml();
使用经过验证和测试的HTML白名单库,如。正则表达式不适合解析和修改HTML。仅供参考,如果您认为使用style
属性对您的网站不好,请等到人们使用XSS时再使用onload
属性来运行javascript。您可以使用一点提示:“#”(]+?)(style=“[^”]”)([^>]+?>)#i'
=“”$1$3“
@Termis他不想剥去标签,只想attrs@bwoebi哦对不起,我误解了!
<?php
$doc = new DOMDocument();
$doc->loadHTML($html);
$images = $doc->getElementsByTagName('img');
foreach ($images as $image) {
$image->removeAttributeNode('style');
}
$cleanHtml = $doc->saveHtml();