这是一个功能,可以让你去除所有的属性,除了那些你想:
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('
');
或如果你例如。要保持“class”属性:
echo stripAttributes('
', array('class'));
或者
假设你将消息发送到收件箱和你CKEDITOR组成你的消息,你可以如下分配功能,并将它显示给$ message变量在发送之前。请注意,名称为stripAttributes()的函数将去掉所有不必要的html标签。我试过了,它工作正常。我只看到了我加入的格式,如粗体e.t.c.
$message = stripAttributes($_POST['message']);
或 可以echo $message;预览。