tags:

views:

85

answers:

2

Hello,

How can I get content of a DIV using regular expression. What I need to get is in between;

<div class="lv1right dfbg">......</div>

Also there are several (not well defined) tags between these div tags, and I would like to get all of them..

Thanks in advance...

+5  A: 

I'd avoid using regexps for HTML, since HTML isn't regular. Instead check out this PHP DOM parser, which will allow you to extract/change the HTML much more reliably.

Brian Agnew
Second, HTML is too irregular for regular expressions
Mike B
+1 first Don't Process HTML With Regex post of the day. Many more to come, I'm sure.
bobince
HTML is not always perfect but browsers render it anyway. This would make regex not feasible.
Collin Price
A: 
(?<=<div.*>).*?(?=</div>)
xoxo
Will this work for nested div tags? <div class="lv1right dfbg"><div></div></div>
Mike B
Don't think this will work for nested divs.
Trumpi
To get the nested ones could you not apply a recursive method that applies this regex (edited to: (?<=<div.*>).*(?=</div>)) to the match string(being the match value from the previous match).
xoxo
No making it greedy will not cause it to match nested divs. It merely causes to match everything between the first `div` and the last `</div>` to be matched (iff DOT-ALL is enabled: the DOT does not match line breaks by default).
Bart Kiers