Match nested div with preg match

i need to match all div / table block in the web page.
i usually use


preg_match_all("/(.*)<(div|table)([^<>]*)>(.*)<\\/(div|table)>(.*)/iUs",$s,$m); 

but this way went wrong with nested div


<div>a<div>b</div></div>

~> my code returns with the first </div>

<div>a<div>b</div>

so, how can i get <div>a<div>b</div></div> (biggest div block) in every situation?

Maybe look at using phpQuery - jQuery port to PHP ? Allows you to use jQuery style selectors - rather than lots of regex.

PHP: DOM - Manual is also available, but I ended up with enormous amounts of code to do just simple things.

Is there any reason why you want to use regular expressions? There are far more competent, easier to work with, robust tools available for working with HTML documents are you are.

If you haven’t already, and aren’t restricted to using regex, have a look at the DOM. If that sounds like a good idea, or you want to stick with regex, let us know.