对于有效的html,需要有效的div选择正则表达式

时间:2013-04-26 18:14:36

标签: html regex

我想使用正则表达式对html有效,我需要div选择的正则表达式。

我用过这个

      (<div.*?>.*?<\/div>)

但问题在那里,它也有效这种文字:

 <div>some this <div> some another text</div>

无效

我需要那种只给我最后一部分的表达

<div> some another text</div>

请指教我

1 个答案:

答案 0 :(得分:2)

是的,在大多数情况下,使用正则表达式解析html并不是一个好方法。更好的方法是使用DOMDocument,XPath ......

不幸的是,某些标记语言没有机会拥有所有这些工具。这是火星标记语言的情况,必须使用正则表达式进行解析(它在火星上是必须的,它是用圣经写的)

<meta charset="UTF-8"/><pre>
<?php // this take the content between the most inner tags ͽΛΙPͼ
$subject = 'ͽΛΙPͼ  ŏoo͢o öo ͽΛΙPͼ  o̊őoo͟o o͇o͈o͉ o̍o̎o ͽ/ΛΙPͼ  o̐oo͜oo ͽ/ΛΙPͼ';
$pattern = '~(?<=ͽΛΙPͼ)(?:[^ͽ]++|ͽ(?!/?ΛΙPͼ))*+(?=ͽ/ΛΙPͼ)~u';
preg_match_all($pattern, $subject, $matches);
print_r($matches);