Vinod VT
Vinod VT

Reputation: 7149

Regular expression to extract full content inside a div

How to extract the full html content inside a div ? I tried this code,

$html= '<html>
            <body>
                <div id="test">
                    <div id="mydiv1">Hello</div>
                    <div id="mydiv2">How are you</div>
                </div>
            </body>
        </html>';

$attr = "id";
$value = "test";

$tag_regex = '/<div[^>]*'.$attr.'="'.$value.'">(.*?)<\\/div>/si';
preg_match($tag_regex,$html,$matches);

echo $matches[0];

By running this code I get the result,

 <div id="test">
    <div id="mydiv1">Hello</div>

Expected result,

<div id="test">
   <div id="mydiv1">Hello</div>
   <div id="mydiv2">How are you</div>
</div>

In my code the regular expression execute till the first occurrence of </div> . How can I get the full code inside <div id="test"> ?

Upvotes: 3

Views: 417

Answers (1)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

With DOMDocument:

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

$div = $dom->getElementById('test');

$result = $dom->saveHTML($div);

Upvotes: 4

Related Questions