13
A
回答
5
HTML Purifier是用PHP清理HTML的更好的工具之一。
14
虽然有更好的方法,你实际上可以剥去HTML标签参数使用正则表达式:
<?php
function stripArgumentFromTags($htmlString) {
$regEx = '/([^<]*<\s*[a-z](?:[0-9]|[a-z]{0,9}))(?:(?:\s*[a-z\-]{2,14}\s*=\s*(?:"[^"]*"|\'[^\']*\'))*)(\s*\/?>[^<]*)/i'; // match any start tag
$chunks = preg_split($regEx, $htmlString, -1, PREG_SPLIT_DELIM_CAPTURE);
$chunkCount = count($chunks);
$strippedString = '';
for ($n = 1; $n < $chunkCount; $n++) {
$strippedString .= $chunks[$n];
}
return $strippedString;
}
?>
上面也许可以在更少的字符写的,但它的工作(快速和肮脏的) 。
-1
你也可以看看html净化器。诚然,它非常臃肿,如果它只是想象这个具体的例子,它可能不适合你的需求,但它或多或少地为可能的敌对html提供“防弹”净化。你也可以选择允许或禁止某些属性(它是高度可配置的)。
9
地带属性(在PHP5标准)使用SimpleXML
<?php
// define allowable tags
$allowable_tags = '<p><a><img><ul><ol><li><table><thead><tbody><tr><th><td>';
// define allowable attributes
$allowable_atts = array('href','src','alt');
// strip collector
$strip_arr = array();
// load XHTML with SimpleXML
$data_sxml = simplexml_load_string('<root>'. $data_str .'</root>', 'SimpleXMLElement', LIBXML_NOERROR | LIBXML_NOXMLDECL);
if ($data_sxml) {
// loop all elements with an attribute
foreach ($data_sxml->xpath('descendant::*[@*]') as $tag) {
// loop attributes
foreach ($tag->attributes() as $name=>$value) {
// check for allowable attributes
if (!in_array($name, $allowable_atts)) {
// set attribute value to empty string
$tag->attributes()->$name = '';
// collect attribute patterns to be stripped
$strip_arr[$name] = '/ '. $name .'=""/';
}
}
}
}
// strip unallowed attributes and root tag
$data_str = strip_tags(preg_replace($strip_arr,array(''),$data_sxml->asXML()), $allowable_tags);
?>
7
这是一个功能,可以让你去除所有的属性,除了那些你想:
function stripAttributes($s, $allowedattr = array()) {
if (preg_match_all("/<[^>]*\\s([^>]*)\\/*>/msiU", $s, $res, PREG_SET_ORDER)) {
foreach ($res as $r) {
$tag = $r[0];
$attrs = array();
preg_match_all("/\\s.*=(['\"]).*\\1/msiU", " " . $r[1], $split, PREG_SET_ORDER);
foreach ($split as $spl) {
$attrs[] = $spl[0];
}
$newattrs = array();
foreach ($attrs as $a) {
$tmp = explode("=", $a);
if (trim($a) != "" && (!isset($tmp[1]) || (trim($tmp[0]) != "" && !in_array(strtolower(trim($tmp[0])), $allowedattr)))) {
} else {
$newattrs[] = $a;
}
}
$attrs = implode(" ", $newattrs);
$rpl = str_replace($r[1], $attrs, $tag);
$s = str_replace($tag, $rpl, $s);
}
}
return $s;
}
在示例它会是:
echo stripAttributes('<p class="one" otherrandomattribute="two">');
或如果你例如。要保持“class”属性:
echo stripAttributes('<p class="one" otherrandomattribute="two">', array('class'));
或者
假设你将消息发送到收件箱和你CKEDITOR组成你的消息,你可以如下分配功能,并将它显示给$ message变量在发送之前。请注意,名称为stripAttributes()的函数将去掉所有不必要的html标签。我试过了,它工作正常。我只看到了我加入的格式,如粗体e.t.c.
$message = stripAttributes($_POST['message']);
或 可以echo $message;
预览。
5
我诚实地认为唯一的方法是使用标签和属性白名单与HTML Purifier库。示例脚本这里:
<html><body>
<?php
require_once '../includes/htmlpurifier-4.5.0-lite/library/HTMLPurifier/Bootstrap.php';
spl_autoload_register(array('HTMLPurifier_Bootstrap', 'autoload'));
$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.Allowed', 'p,b,a[href],i,br,img[src]');
$config->set('URI.Base', 'http://www.example.com');
$config->set('URI.MakeAbsolute', true);
$purifier = new HTMLPurifier($config);
$dirty_html = "
<a href=\"http://www.google.de\">broken a href link</a
fnord
<x>y</z>
<b>c</p>
<script>alert(\"foo!\");</script>
<a href=\"javascript:alert(history.length)\">Anzahl besuchter Seiten</a>
<img src=\"www.example.com/bla.gif\" />
<a href=\"http://www.google.de\">missing end tag
ende
";
$clean_html = $purifier->purify($dirty_html);
print "<h1>dirty</h1>";
print "<pre>" . htmlentities($dirty_html) . "</pre>";
print "<h1>clean</h1>";
print "<pre>" . htmlentities($clean_html) . "</pre>";
?>
</body></html>
我们得到以下的清洁,符合标准的HTML片段:
<a href="http://www.google.de">broken a href link</a>fnord
y
<b>c
<a>Anzahl besuchter Seiten</a>
<img src="http://www.example.com/www.example.com/bla.gif" alt="bla.gif" /><a href="http://www.google.de">missing end tag
ende
</a></b>
在你的情况下,白名单是:
$config->set('HTML.Allowed', 'p');
相关问题
- 1. 从HTML标签中删除属性
- 2. PHP如何仅从img标签中的HTML标签中删除样式属性?
- 3. 从标签中删除事件属性
- 4. 当不应显示属性时删除HTML标签属性
- 5. 如何删除HTML标签的所有属性
- 6. 如何删除默认的HTML标签属性在IE7
- 7. 如何从标签中删除的href属性和使用jquery
- 8. 删除标签中的属性
- 9. 如何在PHP中删除html标签?
- 10. 删除html标签
- 11. 删除HTML标签
- 12. 从UIWebView中删除html标签
- 13. 从Python DataFrame中删除HTML标签
- 14. 从drupal令牌中删除html标签
- 15. 从BeautifulSoup解析HTML中删除标签
- 16. 从html输入中删除标签sed
- 17. HTML属性中的标签?
- 18. 从图像标签的标题和alt属性中去除HTML标签
- 19. PHP DOM文档删除标签属性
- 20. 删除div标签上的Style属性
- 21. 删除图例标签的属性
- 22. 如何删除标签<input type =“file”>中的属性“
- 23. 从rails中的属性中删除所有html标记
- 24. 如何从字符串中删除HTML标签不JS中
- 25. 如何从android中的sqlite数据库中删除html标签?
- 26. 删除HTML标记的属性
- 27. 如何删除一些html标签?
- 28. 如何从TabHost中删除标签
- 29. 如何从Twitter标签中删除#?
- 30. 如何从标签中删除数据
这个伟大的工程,只不过如果你的输入html格式正确的话就是xml。否则,在分析之前,您必须对输入html进行一些预清理。如果你不完全控制源html输入,这可能是非常繁琐的消毒。 – 2015-08-18 04:20:24