PHP DOM 用新元素替换元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3194875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:58:44  来源:igfitidea点击:

PHP DOM replace element with a new element

phpdom

提问by Richard Knop

I have a DOM object with loaded HTML markup. I'm trying to replace all embed tags that look like this:

我有一个加载了 HTML 标记的 DOM 对象。我正在尝试替换所有看起来像这样的嵌入标签:

<embed allowfullscreen="true" height="200" src="path/to/video/1.flv" width="320"></embed>

With a tag like this:

使用这样的标签:

<a 
href="path/to/video/1.flv" 
style="display:block;width:320px;height:200px;" 
id="player">
</a>

I'm having troubles figuring this out and I don't want to use regular expression for this. Could you help me out?

我在弄清楚这一点时遇到了麻烦,我不想为此使用正则表达式。你能帮我吗?

EDIT:

编辑:

This is what I have so far:

这是我到目前为止:

         // DOM initialized above, not important
            foreach ($dom->getElementsByTagName('embed') as $e) {
                $path = $e->getAttribute('src');
          $width = $e->getAttribute('width') . 'px';
          $height = $e->getAttribute('height') . 'px';
          $a = $dom->createElement('a', '');
          $a->setAttribute('href', $path);
          $a->setAttribute('style', "display:block;width:$width;height:$height;");
          $a->setAttribute('id', 'player');
          $dom->replaceChild($e, $a); // this line doesn't work
      }

回答by bobince

It's easy to find elements from a DOM using getElementsByTagName. Indeed you wouldn't want to go near regular expressions for this.

使用getElementsByTagName. 实际上,您不想为此接近正则表达式。

If the DOM you are talking about is a PHP DOMDocument, you'd do something like:

如果您正在谈论的 DOM 是 PHP DOMDocument,您会执行以下操作:

$embeds= $document->getElementsByTagName('embed');
foreach ($embeds as $embed) {
    $src= $embed->getAttribute('src');
    $width= $embed->getAttribute('width');
    $height= $embed->getAttribute('height');

    $link= $document->createElement('a');
    $link->setAttribute('class', 'player');
    $link->setAttribute('href', $src);
    $link->setAttribute('style', "display: block; width: {$width}px; height: {$height}px;");

    $embed->parentNode->replaceChild($link, $embed);
}

Edit re edit:

编辑重新编辑:

$dom->replaceChild($e, $a); // this line doesn't work

Yeah, replaceChildtakes the new element to replace-with as the first argument and the child to-be-replaced as the second. This is not the way round you might expect, but it is consistent with all the other DOM methods. Also it's a method of the parent node of the child to be replaced.

是的,replaceChild将要替换的新元素作为第一个参数,将要替换的子元素作为第二个参数。这不是您所期望的方式,但它与所有其他 DOM 方法一致。它也是要替换子节点的父节点的一种方法。

(I used classnot id, as you can't have multiple elements on the same page all called id="player".)

(我使用classnot id,因为您不能在同一页面上有多个元素都称为id="player"。)