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

时间:2016-02-19 17:59:47

标签: php html tags

我需要更改<img>代码的<video>代码。一世 不知道如何继续使用代码,因为我可以更改所有标签,只要它们包含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;
}

3 个答案:

答案 0 :(得分:1)

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

但我正在使用for-iteration并使用简单的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.

答案 1 :(得分:0)

使用此代码:

(...)
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标记和nodeValueimg节点值的新节点,然后我将所有img属性添加到新节点,最后我用新节点替换旧节点。

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

答案 2 :(得分:0)

使用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