Shubham
Shubham

Reputation: 22307

How to get tag content using DOMDocument?

Say this is the HTML?

<html>
<body>
<embed scr="...." attr="..."></embed>
</body>
</html>

I want to match whole embed tag <embed scr="...." attr="..."></embed>. How can I do so?

I got this far

$fragment = new DOMDocument();
$fragment->loadHTML($string);

$xp = new DOMXPath($fragment);
$result = $xp->query("//embed");
print_r($result->item(0));

Upvotes: 1

Views: 3560

Answers (2)

Ewout Kleinsmann
Ewout Kleinsmann

Reputation: 1297

You could take a look at this PHP Class.

If I understood your problem correctly. Doing it with this class would be as simple as:

$html = str_get_html($string);
$ret = $html->find('embed');

EDIT. And the same thing in phpQuery:

phpQuery::newDocumentHTML($string);
$ret = pq('embed');

You should look into this post of Gordon by the way.

Upvotes: 2

Caner
Caner

Reputation: 59168

Like this:

<?php
$fragment = new DOMDocument();
$fragment->loadHTML($string);

foreach ($fragment->getElementsByTagName("embed") as $element) 
{
    echo $fragment->saveXML($element);
}
?>

Upvotes: 8

Related Questions