0

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>
Hamza
  • 1,087
  • 4
  • 21
  • 47
  • Regex solution would be difficult, what is the reason why DOM parser is not an option? – insanebits Aug 11 '14 at 13:42
  • Sounds like you should look into jQuery. What you are going to do with the content? http://learn.jquery.com/using-jquery-core/faq/how-do-i-select-an-item-using-class-or-id/ – JonBaron Aug 11 '14 at 13:45
  • It is possible, but only for certain cases. See [this question](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) for more details. – Vatev Aug 11 '14 at 13:45
  • Many clients will use this app, and some won't have dom parser installed by default. – Hamza Aug 11 '14 at 13:46
  • Possible duplicate of http://stackoverflow.com/questions/5355452/using-a-regular-expression-to-match-a-div-block-having-a-specific-id – insanebits Aug 11 '14 at 13:51

2 Answers2

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

If there is no additional div in the content itself.

Johni
  • 2,933
  • 4
  • 28
  • 47
0

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]);
Ram Sharma
  • 8,676
  • 7
  • 43
  • 56