0

Hello

I'm learning regexp and I'm trying to get contents in a div tag.

<div class='name'>
<div class='contents'>
contents
</div>

</div>

my regexp is something like this

preg_match_all("/\<div class=\"name\"\>(.*?)\<\/div\>/is",
		$res, $matches );
   foreach($matches[0] as $value)
{
	echo $value;
}

I want to get everything in the div class='name' but I think that php is going to stop executing after getting the first </div> tag. How do I get the whole div tag?


Thanks

2
Contributors
6
Replies
11
Views
6 Years
Discussion Span
Last Post by andrewliu
0

Regular expressions do not work recursively. To match arbitrarily deeply nested div tags you would need such an recursion. You could use a regex for this special case like this:

preg_match_all('~<div class=\'name\'>\s*(<div.*?</div>\s*)?(.*?)</div>~is', $res, $matches );
0

what if there were multiple div tags inside the div class='name'?

does the

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

take care of all that too? Or that only takes care of one div tag nested inside div class='name'?

Thank you for your help

0

(<div.*?</div>\s*) matches until the first </div> . Therefore you cannot tackle nested divs (or other nested tags) with it. Use a recursive or parse function for that.

0

Thanks!

if there's multiple same div names throughout the page because of a while loop say

[B]<div class='name'>[/B]</div>
<div>code here</div>
etc..
[B]<div class='name'>[/B]</div>
<div>code here</div>
etc...
[B]<div class='name'></div>[/B]
<div>code here</div>
etc...

How do I get all of the same <div class='name'></div>?

Thanks!

0

If divs are not nested you get them with the code from your first post (after fixing the quotes): ~<div class='name'\>(.*?)</div>~s

0

But I only want to get all of the <div class='name'>content</div> only

and i don't want the other stuff. just within <div class='name'></div>. The original code will give me everything?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.