Hamza
Hamza

Reputation: 1085

Get div by id from string using PHP

I have a while dom document string inside variable, I want to grab only content of a div using id, I can't use PHP dom parser, is it possible using Regex?

<html>
....
<body>
....
<div id="something">
// content
</div>

<div id="other_divs">
// content
</div>

</body>
</html>

Upvotes: 0

Views: 1878

Answers (2)

Ram Sharma
Ram Sharma

Reputation: 8809

you can try to use this regexp, hope it will help you.

preg_match_all('/<div\s*id="[^>]+">(.*?)<\/div>/s', $html, $match);  
print_r($match[0]);

Upvotes: 0

Johni
Johni

Reputation: 2961

preg_match('%<div[^>]+id="something"[^>]*>(.*?)</div>%si', $string)

If there is no additional div in the content itself.

Upvotes: 2

Related Questions