dpl22899 2017-04-10 21:58
浏览 47
已采纳

用子节点PHP替换DOMDocument父节点

This is my HTML code and I wanna replace <a> tag with <img> tag using DOMDocument.

<a href='xxx.com'><img src='yyy.jpg'></a>

Here is the PHP code:

$newNode=cj_DOMinnerHTML($link); //$link refer to anchor tag 
$image_dom = new DOMDocument();
$image_dom->loadHTML($newNode);
$link->parentNode->replaceChild($image_dom, $link); //this replace making my parent node empty 

cj_DOMinnerHTML is function which return child nodes as HTML.

  • 写回答

1条回答 默认 最新

  • ds78662302 2017-04-11 08:29
    关注

    Hello_ mate.

    If I understood you well you want to remove the <a> tags and I don't know what exactly your function cj_DOMinnerHTML is doing, but I see that your are passing instance of DOMDocument to replaceChild method as first argument which is wrong. Refer to documentation to see how exactly is replaceChild working (it accepts two arguments of type DOMNode). Anyway I give you a code snippet that is replacing the <a> tags. Please read the comments that I put in code and try to change the code for your use case.

    $html = '
    <div id="container">
        <a href="xxx.com"><img src="yyy.jpg"></a>
        <a href="aaa.com"><img src="aaa.jpg"></a>
        <a href="bbb.com"><img src="bbb.jpg"></a>
        <a href="ccc.com"><img src="ccc.jpg"></a>
        <a href="ddd.com"><img src="ddd.jpg"></a>
        <a href="eee.com"><img src="eee.jpg"></a>
    </div>';
    
    // load the dom document
    $dom = new \DOMDocument();
    if (!$dom->loadHTML($html)) {
        echo '<h2>Error handle this ...</h2>';
    }
    
    // instantiate DOMXPath object
    $finder = new \DOMXPath($dom);
    
    // get all <a> tags of element that has id="container"
    $anchors = $finder->query("//*[contains(concat(' ', normalize-space(@id), ' '), 'container')]/descendant::a");
    
    // loop through all <a>
    foreach ($anchors as $a) {
        $parent = $a->parentNode;
        // the following row of code will actually remove the <a> tag
        $parent->replaceChild($a->childNodes->item(0), $a);
    }
    
    // show output
    echo htmlspecialchars($dom->saveHTML());
    

    OUTPUT

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> 
    <html>
        <body>
            <div id="container"> 
                <img src="yyy.jpg"> 
                <img src="aaa.jpg"> 
                <img src="bbb.jpg"> 
                <img src="ccc.jpg"> 
                <img src="ddd.jpg"> 
                <img src="eee.jpg"> 
            </div>
        </body>
    </html> 
    

    I hope you will understand the code and you will be able to modify it to work for your needs.

    Good luck friend!

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 优博讯dt50巴枪怎么提取镜像
  • ¥30 在CodBlock上用c++语言运行
  • ¥15 求C6748 IIC EEPROM程序固化烧写算法
  • ¥50 关于#php#的问题,请各位专家解答!
  • ¥15 python 3.8.0版本,安装官方库ibm_db遇到问题,提示找不到ibm_db模块。如何解决?
  • ¥15 TMUXHS4412如何防止静电,
  • ¥30 Metashape软件中如何将建模后的图像中的植被与庄稼点云删除
  • ¥20 机械振动学课后习题求解答
  • ¥15 IEC61850 客户端和服务端的通讯机制
  • ¥15 MAX98357a(关键词-播放音频)
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部