PHP - DOMDocument - 需要更改/替换带有新标签的现有HTML标签


PHP - DOMDocument - need to change/replace an existing HTML tag w/ a new one

我需要为<video>标签更改<img>标签。我不知道如何继续代码,因为我可以更改所有标签,前提是它们包含 WebM。

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;
}

像罗马一样,我正在使用 http://php.net/manual/en/domnode.replacechild.php

但是我正在使用一个简单的 strpos() 在 SRC 中使用迭代和测试.webm扩展。

$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.

使用以下代码:

(...)
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 );
}
(...)

通过上面的代码,我创建了一个带有video标签的新节点,nodeValue img节点值,然后将所有img属性添加到新节点,最后我用新节点替换旧节点。

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

具有DOMDocument::createElementDOMNode::replaceChild函数的解决方案:

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>
                $video = $Dom->createElement('video');
                $video->setAttribute("src", $href);
                $video->setAttribute("controls", '');
                $link->parentNode->replaceChild($video, $link);
            }        
        }
    }
    $html = $Dom->saveHTML();
    return $html;
}

http://php.net/manual/en/domdocument.createelement.php

http://php.net/manual/en/domnode.replacechild.php