php 处理 html 标签属性

function ClearHtml($content) {
//    $content = preg_replace("/<a[^>]*>/i", "", $content);
//    $content = preg_replace("/<\/a>/i", "", $content);
//    $content = preg_replace("/<div[^>]*>/i", "", $content);
//    $content = preg_replace("/<\/div>/i", "", $content);
//    $content = preg_replace("/<!--[^>]*-->/i", "", $content);//注释内容
    $content = preg_replace("/height:.+?['| ']/i",'',$content);//去除样式
//    $content = preg_replace("/style=.+?['|\"]/i",'',$content);//去除样式
//    $content = preg_replace("/class=.+?['|\"]/i",'',$content);//去除样式
//    $content = preg_replace("/id=.+?['|\"]/i",'',$content);//去除样式
//    $content = preg_replace("/lang=.+?['|\"]/i",'',$content);//去除样式
    $content = preg_replace("/width=.+?['|\"]/i",'',$content);//去除样式
    $content = preg_replace("/height=.+?['|\"]/i",'',$content);//去除样式
//    $content = preg_replace("/border=.+?['|\"]/i",'',$content);//去除样式
//    $content = preg_replace("/face=.+?['|\"]/i",'',$content);//去除样式
//    $content = preg_replace("/face=.+?['|\"]/",'',$content);//去除样式只允许

//    小写正则匹配没有带 i 参数
   return $content;
}


$str ='<img href="#" ALT="ceshi" height="30" style="height: 20px;" >';
echo htmlspecialchars($str)."<br>";
//echo htmlspecialchars(preg_replace("/(.*)(alt=[\"\']?.*[\"\']?\s+)(.*)/i","\${1}\${3}",$str));//要确保ALT="ceshi"后有空格 否则无法匹
//echo htmlspecialchars(preg_replace("/(.*)(alt=[\"\']?.*[\"\']?\s+)(.*)/i","\${1}\${3}",$str));//要确保ALT="ceshi"后有空格 否则无法匹
echo preg_replace("/(.*)(alt=[\"\']?.*[\"\']?\s+)(.*)/i","\${1}\${3}",$str);
//str_replace(\"style=\"(.*)\"0\"","".$str);

猜你喜欢

转载自blog.csdn.net/lemqs0123/article/details/53316971