Home > php教程 > PHP源码 > php正则表达式过滤html标签属性

php正则表达式过滤html标签属性

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-08 17:20:02
Original
1002 people have browsed it

过滤html标签在php中可以有内置的函数了,但它过滤的太干净了,我们就整理了一下些利用正则来过滤指定html标签的例子,具体如下所示。

<script>ec(2);</script>

采集的时候有时候需要过滤掉多余的标签属性,比如 img标签过滤掉除了src属性之外的所有属性例如删除titile alt等属性以及一些脚的onclick属性等。

例如过滤除了src之外的所有属性

$str= preg_replace('/\s(?!src)[a-zA-Z]+=[\'\"]{1}[^\'\"]+[\'\"]{1}/iu',' $str);

上面的实例代码是过滤掉除了src属性外的所有标签属性

过滤设置过滤除了alt和src之外的所有属性,代码如下:

$str = preg_replace('/\s(?!(src|alt))[a-zA-Z]+=[^\s]*/iu',' ', $str);

过滤所有html标签的属性的正则表达式:

$str = preg_replace("/]*>/i","",$str );

只过滤alt属性的正则表达式:

(\s)alt=[^\s]*

过滤所有html标签的属性的正则表达式


$search = array ("'<script>]*?>.*?</script>'si",  // 去掉 javascript
                  "']*?>'si",          // 去掉 HTML 标记
                  "'([\r\n])[\s]+'",                // 去掉空白字符
                  "'&(quot|#34);'i",                // 替换 HTML 实体
                  "'&(amp|#38);'i",
                  "'&(lt|#60);'i",
                  "'&(gt|#62);'i",
                  "'&(nbsp|#160);'i"
                  );                    // 作为 PHP 代码运行
                  $replace = array ("","","\\1","\"","&",""," ");
                  $html = preg_replace($search, $replace, $html);

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template