[分享]正则表达式分析/提取XML/XHTML标签属性
直接看代码吧, 有什么不足请各位大神指教..
- PHP code
<!---ecms Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?php#author: selfimpr#blog: http://blog.gaodaima.com/lgg201#mail: [email protected]$pattern = <<< eot/< #开始标签 (?P(?>\w+)) #标签名 (?P #单个属性子组 (?>\s+) #前置空白 (?P\w+) #属性名 = #赋值符号 (?P(?P')|(?P")) #读取包裹属性值的引号 (?P #值处理子组 (?: #将值分解处理: 分解为1) 偶数个转义字符的部分; 2) 单个转义字符+引号或非当前使用引号的字符 (?P(?>(?:\\\\\\\\)*)*) #消耗掉当前位置起偶数个转义字符 (?P #非转义字符自身的处理 \\\\(?P=quote)|(?(5)[^']|[^"]) #这里用了分支, 一边是转义字符+引号, 另一边是条件匹配的非当前引号字符 ) )* #对值进行的分组进行0次或多次处理 ) (?P=quote) #引号闭合 )* #属性的重复 \s* #后置空白\/?> #标签闭合处理(这里的焦点在于对属性值的处理, 所以没有对的方式进行处理)/xeot;#示例输入$content1 = <<eot;$content2 = <<eot;echo $pattern . chr(10);preg_match($pattern, $content1, $matches);print_r($matches);preg_match($pattern, $content2, $matches);print_r($matches);
——解决方案——————–
没人甩你,我来支持一下。
——解决方案——————–
嗯,一般lgg201上的都是干货….
待会有空测一下,非找个bug出来….
——解决方案———
本&文来源gaodai^.ma#com搞#代!码网
搞gaodaima代码———–
yeah!
$content1 = <<< eot
eot;
这个没出来
——解决方案——————–
Warning: preg_match() [function.preg-match]: Unknown modifier ‘t’ in ….
——解决方案——————–
占楼分割线
——解决方案——————–
嗯,这下突破不了了….
坦白说,这正则还没看懂,有空的时候学习下~~
——解决方案——————–
to lgg201,请教
能否简单说明,大概什么场合你需要这种匹配呢?