如何返回DOMDocument的外部html?

发布于 2024-10-25 06:15:39 字数 448 浏览 6 评论 0原文

我正在尝试替换字符串内的视频链接 - 这是我的代码:

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

不幸的是,当我尝试获取像 < 这样的完整超链接的 html 代码时, outerHTML 不起作用。 a href='http://www.myurl.com/video.flv'>

有什么想法如何实现这一点吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

各空 2024-11-01 06:15:39

从 PHP 5.3.6 开始,您可以将节点传递给 saveHtml,例如,

$domDocument->saveHtml($nodeToGetTheOuterHtmlFrom);

以前版本的 PHP 没有实现这种可能性。您必须使用 saveXml(),但这会创建符合 XML 的标记。对于 元素,这应该不是问题。

请参阅 http://blog .gordon-oheim.biz/2011-03-17-DOM-Goodie-in-PHP-5.3.6/

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/

囚你心 2024-11-01 06:15:39

您可以在 DOM 部分<的用户注释中找到一些建议PHP 手册的 /strong>

例如,这是 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;
}
?> 

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;
}
?> 
肥爪爪 2024-11-01 06:15:39

最好的解决方案是定义您自己的函数,它将返回您的outerhtml:

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

而不是您可以在代码中使用的

echo outerHTML($link); 

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); 
﹉夏雨初晴づ 2024-11-01 06:15:39

将包含 href 的文件重命名为 links.html 或 links.html,以表示其中包含 flv 的 google.com/fly.html,或者将 flv 更改为 wmv 等您想要的 href(如果还有其他 href)
它也会把它们捡起来

  <?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");
?>

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");
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文