PHP去除html的宽高属性的正则表达式

PHP去除html的宽高属性的正则表达式

<pre>
<?php
/**
* 清除宽高样式
* @param String $content 内容
* @return String
*/
function clear_wh($content){
$config = array('width', 'height');

foreach($config as $v){
//匹配 width=400 height = 200这种类型的
$content = preg_replace('/'.$v.'\s*=\s*\d+\s*/i', '', $content);
//匹配 width="500" height="300" 这种类型的
$content = preg_replace('/'.$v.'\s*=\s*.+?["\']/i', '', $content);
//匹配 Width : 100px ; Height: 100 px;
$content = preg_replace('/'.$v.'\s*:\s*\d+\s*px\s*;?/i', '', $content);
}

return $content;
}
?>

<?php
$html = <<<HTML
<div style="text-align:center" width="500" height="300">
<div style="Width : 100px ; Height: 100 px;">
<img src="/images/test.jpg" width=400 height = 200>
<div style="float:left; width: 100px; height : 200 px;"></div>
</div>
<div style="width : 100 px ;height: 100px">
<img src="/images/test.jpg" width=400 height = 200>
</div>
</div>
HTML;

echo '<xmp>';
echo '原内容:'.PHP_EOL;
echo $html.PHP_EOL.PHP_EOL;
echo '过滤后内容:'.PHP_EOL;
echo clear_wh($html);
echo '</xmp>';
?>
</pre>

猜你喜欢

转载自www.cnblogs.com/newmiracle/p/11864751.html