PHP-DOMDocument-需要更改/替换现有HTML标记和新标记

PHP-DOMDocument-需要更改/替换现有HTML标记和新标记,php,html,tags,Php,Html,Tags,我需要更改 } } } $html=$Dom->saveHTML(); 返回$html; } 使用以下代码: function iframe($text) { $Dom = new DOMDocument; libxml_use_internal_errors(true); $Dom->loadHTML($text); $links = $Dom->getElementsByTagName('img'); foreach ($l

我需要更改

}        
}
}
$html=$Dom->saveHTML();
返回$html;
}
使用以下代码:

function iframe($text) {
    $Dom = new DOMDocument;
    libxml_use_internal_errors(true);
    $Dom->loadHTML($text);
    $links = $Dom->getElementsByTagName('img');
    foreach ($links as $link) {
        $href = $link->getAttribute('src');
        if (!empty($href)) {
            $pathinfo = pathinfo($href);
            if (strtolower($pathinfo['extension']) === 'webm') {
                //If extension webm change tag to <video>
            }        
        }
    }
    $html = $Dom->saveHTML();
    return $html;
}
(…)
if(strtolower($pathinfo['extension'])=='webm')
{
//如果扩展webm将标记更改为
$new=$Dom->createElement('video',$link->nodeValue);
foreach($link->attributes as$attribute)
{
$new->setAttribute($attribute->name,$attribute->value);
}
$link->parentNode->replaceChild($new,$link);
}
(...)
通过上面的代码,我用
video
标记和
nodeValue
作为
img
节点值创建了一个新节点,然后我将所有
img
属性添加到新节点中,最后用新节点替换旧节点


请注意,如果旧节点具有
id
,则代码将产生警告。

具有
DOMDocument::createElement
DOMNode::replaceChild
功能的解决方案:

(...)
if( strtolower( $pathinfo['extension'] ) === 'webm') 
{
    //If extension webm change tag to <video>
    $new = $Dom->createElement( 'video', $link->nodeValue );
    foreach( $link->attributes as $attribute )
    {
        $new->setAttribute( $attribute->name, $attribute->value );
    }
    $link->parentNode->replaceChild( $new, $link );
}
(...)
函数iframe($text){ $Dom=新的DOMDocument; libxml\u使用\u内部错误(true); $Dom->loadHTML($text); $links=$Dom->getElementsByTagName('img'); foreach($links作为$link){ $href=$link->getAttribute('src'); 如果(!empty($href)){ $pathinfo=pathinfo($href); if(strtolower($pathinfo['extension'])=='webm'){ //如果扩展webm将标记更改为 $video=$Dom->createElement('video'); $video->setAttribute(“src”,$href); $video->setAttribute(“控件”和“”); $link->parentNode->replaceChild($video,$link); } } } $html=$Dom->saveHTML(); 返回$html; }

就像我用的罗马语一样

但是我在src中使用for迭代和测试
.webm
扩展,并使用一个简单的strpos()

$contents=length-1$i>=0$我——){
$nodePre=$images->item($i);
$src=$nodePre->getAttribute('src');
//在src中搜索“.webm”
if(strpos($src,'.webm')!==false){
$nodeVideo=$dom->createElement('video');
$nodeVideo->setAttribute(“src”,$src);
$nodeVideo->setAttribute(“控件”和“”);
$nodePre->parentNode->replaceChild($nodeVideo,$nodePre);
}
}
$html=$dom->saveHTML();
返回$html;
};
echo iframe($contents);
部分输出:

$contents = <<<STR
this is some HTML with an <img src="test1.png"/> in it.
this is some HTML with an <img src="test2.png"/> in it.
this is some HTML with an <img src="test.webm"/> in it,
but it should be a video tag - when iframe() is done.
STR;

function iframe($text)
{
    $dom = new DOMDocument;
    libxml_use_internal_errors(true);
    $dom->loadHTML($text);

    $images = $dom->getElementsByTagName("img");

    for ($i = $images->length - 1; $i >= 0; $i --) {
        $nodePre = $images->item($i);
        $src     = $nodePre->getAttribute('src');
        // search in src for ".webm"
        if(strpos($src, '.webm') !== false ) {

            $nodeVideo = $dom->createElement('video');
            $nodeVideo->setAttribute("src", $src);
            $nodeVideo->setAttribute("controls", '');

            $nodePre->parentNode->replaceChild($nodeVideo, $nodePre);
        }
    }

    $html = $dom->saveHTML();
    return $html;
};

echo iframe($contents);
这是一个带有,
但当iframe()完成时,它应该是一个视频标记。

在第二个agragado标记属性控件中,没有显示,因此添加一行以正常工作$video->setAttribute(“控件”,“”);
$contents = <<<STR
this is some HTML with an <img src="test1.png"/> in it.
this is some HTML with an <img src="test2.png"/> in it.
this is some HTML with an <img src="test.webm"/> in it,
but it should be a video tag - when iframe() is done.
STR;

function iframe($text)
{
    $dom = new DOMDocument;
    libxml_use_internal_errors(true);
    $dom->loadHTML($text);

    $images = $dom->getElementsByTagName("img");

    for ($i = $images->length - 1; $i >= 0; $i --) {
        $nodePre = $images->item($i);
        $src     = $nodePre->getAttribute('src');
        // search in src for ".webm"
        if(strpos($src, '.webm') !== false ) {

            $nodeVideo = $dom->createElement('video');
            $nodeVideo->setAttribute("src", $src);
            $nodeVideo->setAttribute("controls", '');

            $nodePre->parentNode->replaceChild($nodeVideo, $nodePre);
        }
    }

    $html = $dom->saveHTML();
    return $html;
};

echo iframe($contents);
this is some HTML with an <video src="test.webm"></video> in it,
but it should be a video tag - when iframe() is done.