douba8048 2013-07-28 17:04
浏览 54
已采纳

如何使用php dom文档删除和替换标签

I have image tag <img src="path_to_file.png"> but I want that the image tag be converted to link in mobile site. So I want img to be converted to an href:

<a href="path_to_file.png" target="_blank">Click here to open in new tab</a>

I am getting started with php dom. I could get all the attribute listed.

$newdocument = new DOMDocument();
$newdocument->loadHTML();
$getimagetag = $doc->getElementsByTagName('img');
foreach($getimagetag as $tag) {
    echo $src=$tag->getAttribute('src');
}

But how do we get the src attribute , then remove the img tag completely because it contains other parameter like height and length and then create new tag of link?

Hi guys I could get it done from php dom using following code

$input="<img src='path_to_file.png' height='50'>";
    $doc = new DOMDocument();
    $doc->loadHTML($input);
    $imageTags = $doc->getElementsByTagName('img');
    foreach($imageTags as $tag) {
        $src=$tag->getAttribute('src'); 
        $a=$doc->createElement('a','click here to open in new tab');
        $a->setAttribute('href',$src);
        $a->setAttribute('style','color:red;');
        $tag->parentNode->replaceChild($a,$tag);
        } 
        $input=$doc->saveHTML();
echo $input; 

The create element can also be used to put text between <a></a> ie Click...new tab.

replacechild is used to remove $tag i.e. img and replace it with a tag. By setting attribute, we can add other parameters like style,target etc.

I used php dom in the end because I only wanted the data that I get from mysql to be converted and not the other elements like logo of website. Ofcourse it can be possible using javascript too.

Thanks

@dave chen for javascript way and pointing to detecting mobile link.

@nate for pointing me to a answer.

  • 写回答

2条回答 默认 最新

  • doupi1532 2013-07-28 17:29
    关注

    I would recommend doing this with JavaScript:

    <!DOCTYPE html>
    <html>
    <head>
        <title>Images Test</title>
        <script>
            window.onload = changeImages;
    
            function changeImages() {
                var images = document.getElementsByTagName("img");
    
                while (images.length > 0) {
                    var imageLink       = document.createElement("a");
                    imageLink.href      = images[0].src;
                    imageLink.innerHTML = "Click here to view " + images[0].title;
                    images[0].parentNode.replaceChild(imageLink, images[0]);
                }
            }
        </script>
    </head>
    <body>
        Here is a image of flowers  : <img src="images/flowers.bmp"   title="Flowers"  ><br>
        Here is a image of lakes    : <img src="images/lakes.bmp"     title="Lakes"    ><br>
        Here is a image of computers: <img src="images/computers.bmp" title="Computers"><br>
    </body>
    </html>
    

    Example

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?