1

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));
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Shubham
  • 21,300
  • 18
  • 66
  • 89
  • You cannot dump DOMElement's or any other of the DOM objects. Pass the node to saveXML or saveHTML to get the outerHTML. – Gordon Jul 28 '11 at 13:36
  • possible duplicate of [PHP + DOMDocument: outerHTML for element?](http://stackoverflow.com/questions/5404941/php-domdocument-outerhtml-for-element) – Gordon Jul 28 '11 at 13:36

2 Answers2

8

Like this:

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

foreach ($fragment->getElementsByTagName("embed") as $element) 
{
    echo $fragment->saveXML($element);
}
?>
Caner
  • 57,267
  • 35
  • 174
  • 180
2

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.

Community
  • 1
  • 1
Ewout Kleinsmann
  • 1,287
  • 1
  • 9
  • 20