-1

For example i've this kind of content

<div id="t1" class="tt" tag='t2"><div class="t3">tee</div><a href='#'>test</a><span>test</span><div>asdf</div></div>

<div id="t1" class="tt" tag='t2"><div class="t3">tee</div><a href='#'>test</a><span>test</span><div>asdf</div></div>

I am trying to use preg_match to get the content between parent div, so here the parent divs means <div id="t1". I do i use preg_match or is there any other way to get the data between those divs?

000
  • 26,951
  • 10
  • 71
  • 101

2 Answers2

5

A regular expression is the wrong tool for this job. You want a DOM parser.

$dom = new DOMDocument;
$dom->loadHTML($html);

$t1 = $dom->getElementById('t1');
echo $t1->nodeValue;

This will only return you the text, if you want the innerHTML, try this:

$dom = new DOMDocument;
$dom->loadHTML($html);

$t1 = $dom->getElementById('t1');

$innerHTML = '';
foreach($t1->childNodes as $child){
    $innerHTML .= $dom->saveHTML($child);
}
echo $innerHTML;
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
1

Don't attempt to parse HTML with regex: Using regular expressions to parse HTML: why not?

Use a PHP DOM library like http://simplehtmldom.sourceforge.net/

Community
  • 1
  • 1