DOM替换元素与新元素

DOM替换元素与新元素

本文介绍了PHP DOM替换元素与新元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有加载HTML标记的DOM对象。我正在替换所有这样的嵌入标签:

 < embed allowfullscreen =trueheight =200 src =path / to / video / 1.flvwidth =320>< / embed> 

使用如下标签:

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

我有麻烦弄清楚这一点,我不想使用正则表达式。你可以帮我吗?



编辑:



这是我到目前为止:

  //上面已经初始化了DOM,不重要
foreach($ dom-> getElementsByTagName('embed')as $ e){
$ path = $ e-> getAttribute('src');
$ width = $ e-> getAttribute('width')。 像素;
$ height = $ e-> getAttribute('height')。 像素;
$ 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); //这行不起作用
}


解决方案

p>使用 getElementsByTagName 可以轻松地从DOM中找到元素。实际上,你不会想要接近这个正则表达式。



如果你正在谈论的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);
}

编辑修改:

  $ dom-> replaceChild($ e,$ a); //这行不起作用

是的, replaceChild 将新元素替换为第一个参数,并将子对象替换为第二个参数。这不是你期望的方式,但它与所有其他DOM方法一致。此外,它是一个孩子的父节点的方法被替换。



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


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

<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
      }
解决方案

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

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

$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, replaceChild takes 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.

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

这篇关于PHP DOM替换元素与新元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 15:50