php正则如何匹配HTML标签? php正则如何匹配HTML标签?因为我是直接读取别人源码,然后进行匹配,老是失败,谁能给写个代码,举个例子。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 刚才在网上看了一下这个代码$str = "<table width='200' ><tr align='center'><td>这是一个例子</td></tr></table>";$pattern = "/<[^>]+>(.*)<\/[^>]+>/";不明白[^>]不是取反的意思吗?我将它^去掉,反着写是错的 $pattern = "/<[^>]+>(.*)<\/[^>]+>/";就是配匹<>以外的内容 匹配一个html标签,匹配table如下:<table.*?>[ss]*</table> 或 <table.*?>[ss]*?</table>以上两个表达式,一个加了"?"和一个却不加"?",那么这有什么区别呢? 我们知道"?"在正则表达式里是一个通配符:匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。在这里,通过测试,我们得出这样的结论:在不加"?"的情况下,在匹配下面一段内容的时候:<table>这是第一个table</table> 我不是table里的内容 <table>这是第二个table</table> 我也不是table里的内容 <table>这是第三个table</table><?php$str=preg_replace("/s+/", " ", $str); //过滤多余回车$str=preg_replace("/<[ ]+/si","<",$str); //过滤<__("<"号后面带空格)$str=preg_replace("/<!--.*?-->/si","",$str); //注释$str=preg_replace("/<(!.*?)>/si","",$str); //过滤doctype$str=preg_replace("/<(/?html.*?)>/si","",$str); //过滤html标签$str=preg_replace("/<(/?head.*?)>/si","",$str); //过滤head标签$str=preg_replace("/<(/?meta.*?)>/si","",$str); //过滤meta标签$str=preg_replace("/<(/?body.*?)>/si","",$str); //过滤body标签$str=preg_replace("/<(/?link.*?)>/si","",$str); //过滤link标签$str=preg_replace("/<(/?form.*?)>/si","",$str); //过滤form标签$str=preg_replace("/cookie/si","cookie",$str); //过滤cookie标签$str=preg_replace("/<(applet.*?)>(.*?)<(/applet.*?)>/si","",$str); //过滤applet标签$str=preg_replace("/<(/?applet.*?)>/si","",$str); //过滤applet标签$str=preg_replace("/<(style.*?)>(.*?)<(/style.*?)>/si","",$str); //过滤style标签$str=preg_replace("/<(/?style.*?)>/si","",$str); //过滤style标签$str=preg_replace("/<(title.*?)>(.*?)<(/title.*?)>/si","",$str); //过滤title标签$str=preg_replace("/<(/?title.*?)>/si","",$str); //过滤title标签$str=preg_replace("/<(object.*?)>(.*?)<(/object.*?)>/si","",$str); //过滤object标签$str=preg_replace("/<(/?objec.*?)>/si","",$str); //过滤object标签$str=preg_replace("/<(noframes.*?)>(.*?)<(/noframes.*?)>/si","",$str); //过滤noframes标签$str=preg_replace("/<(/?noframes.*?)>/si","",$str); //过滤noframes标签$str=preg_replace("/<(i?frame.*?)>(.*?)<(/i?frame.*?)>/si","",$str); //过滤frame标签$str=preg_replace("/<(/?i?frame.*?)>/si","",$str); //过滤frame标签$str=preg_replace("/<(script.*?)>(.*?)<(/script.*?)>/si","",$str); //过滤script标签$str=preg_replace("/<(/?script.*?)>/si","",$str); //过滤script标签$str=preg_replace("/网页特效/si","javascript",$str); //过滤script标签$str=preg_replace("/vbscript/si","vbscript",$str); //过滤script标签$str=preg_replace("/on([a-z]+)s*=/si","on\1=",$str); //过滤script标签$str=preg_replace("/&#/si","&#",$str); //过滤script标签,如javascript:alert('aabb)?> "<(p|img)[^>]+>.*?<\/(p|)>" 一个通用的匹配HTML标签的正则<script type="text/javascript">//by dron http://ucren.comvar str = "<br /><br><div><div id=a>无忧脚本<img src=\"http://bbs.51js.com/images/default/logo.gif\" width=\"191\" height='75' border=0 onload=\"if(testver>0 && testver<500)alert('test');\" \n onerror='alert(\"test\")' /><img src=xxx alt=\"hello\njust a test!\"></div><hr ><script type=\"test/javascript\" defer>alert(\"just a test!\");<\/script>Hello.<input type=text value=\"无忧脚本\"><br / ><img \"\" ></ ><!-- 注释 -->< ucren><img alt=' title='\"' /><img src=\"http://bbs.51js.com/images/old51js/logo.gif\" /><!-- 注释>>> -->";var reg = /<(?:(?:\/?[A-Za-z]\w+\b(?:[=\s](['"]?)[\s\S]*?\1)*)|(?:!--[\s\S]*?--))>/g; // 通用标签匹配正则var str1 = str.match(reg);alert(str1.join("\n----------------------------------------------------\n"));</script>正则表达式:/<(?:(?:\/?[A-Za-z]\w+\b(?:[=\s](['"]?)[\s\S]*?\1)*)|(?:!--[\s\S]*?--))>/g $partten = "/<[^>]+>(.*)<\/[^>]+>/";$str = "<<a>里面的内容</<a>";分解开来就是这样子? Mysql连接 先学PHP 还是 AJAX好 我想要的结果是1000 但是为什么结果却是1111101000??急求... 急死了,我要将数据通过按钮存入MYSQL,怎么弄呀?555555 php curl 多线程抓取数据能判断分别来自哪个url吗? 今天突然想到一个问题,PHP里面没有数据库“锁”? 得空的来帮个小忙!!(不管觖不解决中午12:00结贴) 文件上传问题 php和c通过socket通信,如何~? 有关session的问题 一个php web services 的问题 我快不行了.....我保证100分只给一个人. FCKeditor提交后图片路径错误
或
<table.*?>[ss]*?</table>以上两个表达式,一个加了"?"和一个却不加"?",那么这有什么区别呢?
我们知道"?"在正则表达式里是一个通配符:匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。在这里,通过测试,我们得出这样的结论:在不加"?"的情况下,在匹配下面一段内容的时候:<table>这是第一个table</table>
我不是table里的内容
<table>这是第二个table</table>
我也不是table里的内容
<table>这是第三个table</table><?php$str=preg_replace("/s+/", " ", $str); //过滤多余回车
$str=preg_replace("/<[ ]+/si","<",$str); //过滤<__("<"号后面带空格)$str=preg_replace("/<!--.*?-->/si","",$str); //注释
$str=preg_replace("/<(!.*?)>/si","",$str); //过滤doctype
$str=preg_replace("/<(/?html.*?)>/si","",$str); //过滤html标签
$str=preg_replace("/<(/?head.*?)>/si","",$str); //过滤head标签
$str=preg_replace("/<(/?meta.*?)>/si","",$str); //过滤meta标签
$str=preg_replace("/<(/?body.*?)>/si","",$str); //过滤body标签
$str=preg_replace("/<(/?link.*?)>/si","",$str); //过滤link标签
$str=preg_replace("/<(/?form.*?)>/si","",$str); //过滤form标签
$str=preg_replace("/cookie/si","cookie",$str); //过滤cookie标签$str=preg_replace("/<(applet.*?)>(.*?)<(/applet.*?)>/si","",$str); //过滤applet标签
$str=preg_replace("/<(/?applet.*?)>/si","",$str); //过滤applet标签$str=preg_replace("/<(style.*?)>(.*?)<(/style.*?)>/si","",$str); //过滤style标签
$str=preg_replace("/<(/?style.*?)>/si","",$str); //过滤style标签$str=preg_replace("/<(title.*?)>(.*?)<(/title.*?)>/si","",$str); //过滤title标签
$str=preg_replace("/<(/?title.*?)>/si","",$str); //过滤title标签$str=preg_replace("/<(object.*?)>(.*?)<(/object.*?)>/si","",$str); //过滤object标签
$str=preg_replace("/<(/?objec.*?)>/si","",$str); //过滤object标签$str=preg_replace("/<(noframes.*?)>(.*?)<(/noframes.*?)>/si","",$str); //过滤noframes标签
$str=preg_replace("/<(/?noframes.*?)>/si","",$str); //过滤noframes标签$str=preg_replace("/<(i?frame.*?)>(.*?)<(/i?frame.*?)>/si","",$str); //过滤frame标签
$str=preg_replace("/<(/?i?frame.*?)>/si","",$str); //过滤frame标签$str=preg_replace("/<(script.*?)>(.*?)<(/script.*?)>/si","",$str); //过滤script标签
$str=preg_replace("/<(/?script.*?)>/si","",$str); //过滤script标签
$str=preg_replace("/网页特效/si","javascript",$str); //过滤script标签
$str=preg_replace("/vbscript/si","vbscript",$str); //过滤script标签
$str=preg_replace("/on([a-z]+)s*=/si","on\1=",$str); //过滤script标签
$str=preg_replace("/&#/si","&#",$str); //过滤script标签,如javascript:alert('aabb)?>
"<(p|img)[^>]+>.*?<\/(p|)>"
//by dron http://ucren.com
var str = "<br /><br><div><div id=a>无忧脚本<img src=\"http://bbs.51js.com/images/default/logo.gif\" width=\"191\" height='75' border=0 onload=\"if(testver>0 && testver<500)alert('test');\" \n onerror='alert(\"test\")' /><img src=xxx alt=\"hello\njust a test!\"></div><hr ><script type=\"test/javascript\" defer>alert(\"just a test!\");<\/script>Hello.<input type=text value=\"无忧脚本\"><br / ><img \"\" ></ ><!-- 注释 -->< ucren><img alt=' title='\"' /><img src=\"http://bbs.51js.com/images/old51js/logo.gif\" /><!-- 注释>>> -->";var reg = /<(?:(?:\/?[A-Za-z]\w+\b(?:[=\s](['"]?)[\s\S]*?\1)*)|(?:!--[\s\S]*?--))>/g; // 通用标签匹配正则var str1 = str.match(reg);alert(str1.join("\n----------------------------------------------------\n"));</script>正则表达式:/<(?:(?:\/?[A-Za-z]\w+\b(?:[=\s](['"]?)[\s\S]*?\1)*)|(?:!--[\s\S]*?--))>/g