PHP正则表达式(PCRE)去除富文本的一些影响显示的属性和样式
1.去除class属性 2.去除margin-top样式 3.去除margin-bottom样式 4.去除padding样式 5.去除postion样式
//富文本内容
$content = "<p style=\"margin-top: -762px; margin-bottom: 0px; padding: 0px; position: absolute; white-space: nowrap; text-shadow: rgba(0, 0, 0, 0.004) 1px 1px 1px; font-size: 190px; font-family: 宋体, df436a14866fb84ae45c8d3f0010001, 宋体; width: 4571px; height: 190px; line-height: 190px; top: 7472px; left: 1429px; z-index: 74;\">为了美丽的祖国我们将竭尽全力,为了祖国美丽的永生</p><p style=\"margin-top: 0px; margin-bottom: 17px; padding: 0px; font-size: 16px; color: rgb(34, 34, 34);\">阅读已结束,获取文档需</p><p><br/></p>";
//正则规则
$rule_arr = [
'/ class="([^\"]*)"/isU',//去除class样式属性
'/ ?margin-top: *-*[0-9]*px;/isU',//去除margin-top样式
'/ ?margin-bottom: *-*[0-9]*px;/isU',//去除margin-bottom样式
'/ ?padding: *-*[0-9]*px;/isU',//去除padding样式
'/ ?position: *\babsolute|relative|fixed|static|inherit\b;/isU'//去除postion样式
];
//正则替换匹配的项
$content = preg_replace($rule_arr,'', $content);
/*
输出结果为:
<p style="; white-space: nowrap; text-shadow: rgba(0, 0, 0, 0.004) 1px 1px 1px; font-size: 190px; font-family: 宋体, df436a14866fb84ae45c8d3f0010001, 宋体; width: 4571px; height: 190px; line-height: 190px; top: 7472px; left: 1429px; z-index: 74;">为了美丽的祖国我们的永生</p><p style=" font-size: 16px; color: rgb(34, 34, 34);">阅读已结束,获取文档需</p><p><br/></p>
*/