php使用正则去除宽高样式

php使用正则去除宽高样式

内容导读

收集整理的这篇技术教程文章主要介绍了php使用正则去除宽高样式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1835字,纯文字阅读大概需要3分钟

内容图文

因工作需要,需要采集html,并把html内容保存到数据库中。为了避免影响使用,宽高样式需要删除。例如图片和p中的width, height等。

不过采集到的html中,样式的写法各有不同,例如大小写,中间有空格等。

因此使用php正则编写了下面这个方法,对这些奇葩的样式进行过滤。

代码如下:

<?php/** * 清除宽高样式 * @param
String $content 内容 * @return String */function clear_wh($content){

$config = array('width', 'height');

foreach($config as $v){



$content = preg_replace('/'.$v.'s*=s*d+s*/i', '', $content);



$content = preg_replace('/'.$v.'s*=s*.+?["']/i', '', $content);



$content = preg_replace('/'.$v.'s*:s*d+s*pxs*;?/i', '', $content);

}

return $content;}?>

演示:

<?php$html = <<<HTML<p style="text-align:center" width="500" height="300">

<p style="Width : 100px ; Height: 100 px;">



<img src="/images/test.jpg" width=400 height = 200>



<p style="float:left; width: 100px; height : 200 px;"></p>

</p>

<p style="width :
 100 px ;height: 100px">



<img src="/images/test.jpg" width=400 height = 200>

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

输出:

原内容:<p style="text-align:center" width="500" height="300">

<p style="Width : 100px ; Height: 100 px;">



<img src="/images/test.jpg" width=400 height = 200>



<p style="float:left; width: 100px; height : 200 px;"></p>

</p>

<p style="width :
 100 px ;height: 100px">



<img src="/images/test.jpg" width=400 height = 200>

</p></p>过滤后内容:<p style="text-align:center"
>

<p style=" ">



<img src="/images/test.jpg" >



<p style="float:left;
"></p>

</p>

<p style="">



<img src="/images/test.jpg" >

</p></p>

本文讲解了php使用正则去除宽高样式,更多相关内容请关注Gxl网。

相关推荐:

mysql表数据行列转换方法

nginx快速查看配置文件的方法

php 多个一维数组合拼成二维数组的方法

以上就是php使用正则去除宽高样式的详细内容,更多请关注Gxl网其它相关文章!

内容总结

以上是为您收集整理的php使用正则去除宽高样式全部内容,希望文章能够帮你解决php使用正则去除宽高样式所遇到的程序开发问题。 如果觉得技术教程内容还不错,欢迎将网站推荐给程序员好友。

内容备注

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。


本文关键词:

联系我们

在线咨询:点击这里给我发消息

邮件:w420220301@qq.com