16

I'm trying to replace video links inside a string - here's my code:

$doc = new DOMDocument();
$doc->loadHTML($content);
foreach ($doc->getElementsByTagName("a") as $link) 
{
    $url = $link->getAttribute("href");
    if(strpos($url, ".flv"))
    {
        echo $link->outerHTML();
    }
}

Unfortunately, outerHTML doesn't work when I'm trying to get the html code for the full hyperlink like <a href='http://www.myurl.com/video.flv'></a>

Any ideas how to achieve this?

kenorb
  • 155,785
  • 88
  • 678
  • 743
Fuxi
  • 7,611
  • 25
  • 93
  • 139

4 Answers4

23

As of PHP 5.3.6 you can pass a node to saveHtml, e.g.

$domDocument->saveHtml($nodeToGetTheOuterHtmlFrom);

Previous versions of PHP did not implement that possibility. You'd have to use saveXml(), but that would create XML compliant markup. In the case of an <a> element, that shouldn't be an issue though.

See http://blog.gordon-oheim.biz/2011-03-17-The-DOM-Goodie-in-PHP-5.3.6/

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • 2
    The DOMNode also contains the containing DOMDocument so you can do this if you just have the node you want to get the HTML of: $domNode->ownerDocument->saveHTML($domNode); – rooby Jul 16 '14 at 06:43
6

You can find a couple of propositions in the users notes of the DOM section of the PHP Manual.

For example, here's one posted by xwisdom :

<?php
// code taken from the Raxan PDI framework
// returns the html content of an element
protected function nodeContent($n, $outer=false) {
    $d = new DOMDocument('1.0');
    $b = $d->importNode($n->cloneNode(true),true);
    $d->appendChild($b); $h = $d->saveHTML();
    // remove outter tags
    if (!$outer) $h = substr($h,strpos($h,'>')+1,-(strlen($n->nodeName)+4));
    return $h;
}
?> 
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • thanks for that but i'm actually looking for the full OUTER html - the full code including :/ – Fuxi Mar 23 '11 at 12:27
  • @Fuxi judging by the code I'd say you have to set $outer to TRUE then. But there is really no reason to use that function, because you can use `saveXml` and `saveHTML` to get the outerHTML like explained in my answer. – Gordon Mar 23 '11 at 12:31
  • @Gordon thanks for your comment :-) *(And I agree with the "use saveHTML" idea -- if using PHP >= 5.3.6)* – Pascal MARTIN Mar 23 '11 at 12:32
5

The best possible solution is to define your own function which will return you outerhtml:

function outerHTML($e) {
     $doc = new DOMDocument();
     $doc->appendChild($doc->importNode($e, true));
     return $doc->saveHTML();
}

than you can use in your code

echo outerHTML($link); 
StanleyD
  • 2,308
  • 22
  • 20
  • Only for PHP < 5.3.6, after that just provide the node as argument for saveHTML(), like Gordon wrote. No need to create an full document, just to save a single node. – ThW Feb 22 '15 at 15:58
0

Rename a file with href to links.html or links.html to say google.com/fly.html that has flv in it or change flv to wmv etc you want href from if there are other href it will pick them up as well

  <?php
  $contents = file_get_contents("links.html");
  $domdoc = new DOMDocument();
  $domdoc->preservewhitespaces=“false”;
  $domdoc->loadHTML($contents);
  $xpath = new DOMXpath($domdoc);
  $query = '//@href';
  $nodeList = $xpath->query($query);
  foreach ($nodeList as $node){
    if(strpos($node->nodeValue, ".flv")){
      $linksList = $node->nodeValue;
      $htmlAnchor = new DOMElement("a", $linksList);
      $htmlURL = new DOMAttr("href", $linksList);
      $domdoc->appendChild($htmlAnchor);
      $htmlAnchor->appendChild($htmlURL);
      $domdoc->saveHTML();
      echo ("<a href='". $node->nodeValue. "'>". $node->nodeValue. "</a><br />");
    }
  }
echo("done");
?>