# 正则表达式去除 HTML 中的所有注释和属性

**URL:** <https://www.shiqidu.com/t/topic/637>\
**Category:** 开发调优\
**Tags:** 正则表达式\
**Created:** [2019 年9 月 5 日 07:58 UTC](https://www.shiqidu.com/t/topic/637 "2019-09-05T07:58:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

作者： ![Leon](https://www.shiqidu.com/user_avatar/www.shiqidu.com/leon/32/5_2.png) [@Leon](https://www.shiqidu.com/u/Leon)\
发布日期： [2019 年9 月 5 日 07:58 UTC](https://www.shiqidu.com/t/topic/637/1 "2019-09-05T07:58:01Z")

</div>

```php
// 去除所有html注释
$html = preg_replace('/<!--[\w\W\r\n]*?-->/mi', '', $html);
// 去除所有属性 如 style=xxx
$html = preg_replace('/(<[^\s\/>]+)\b[^>]*>/i', "$1>", $html);

```

附 [正则表达式中模式修正符作用详解](https://www.shiqidu.com/d/817)
