php DomDocument添加额外的标签 [英] php DomDocument adds extra tags

查看:122
本文介绍了php DomDocument添加额外的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析文档并获取所有图像标签,并更改不同的内容。

I'm trying to parse a document and get all the image tags and change the source for something different.



    $domDocument = new DOMDocument();

    $domDocument->loadHTML($text);

    $imageNodeList = $domDocument->getElementsByTagName('img');

    foreach ($imageNodeList as $Image) {
      $Image->setAttribute('src', 'lalala');
      $domDocument->saveHTML($Image);
    }

    $text = $domDocument->saveHTML();

$ text最初看起来像这样:

The $text initially looks like this:


<p>Hi, this is a test, here is an image<img src="http://mysite.com/beer.jpg" width="60" height="95" /> Because I like Beer!</p>

这是输出$ text :

and this is the output $text:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>Hi, this is a test, here is an image<img src="lalala" width="68" height="95"> Because I like Beer!</p></body></html>

我正在收集一些额外的标签(html,body和顶部的注释),我不需要。任何方式设置DOMDocument以避免添加这些额外的标签?

I'm getting a bunch of extra tags (html, body, and the comment at the top) that I don't really need. Any way to set up the DOMDocument to avoid adding these extra tags?

谢谢!

推荐答案

DomDocument不幸被阻止,不会让你这样做。尝试这样:

DomDocument is unfortunately retarded and won't let you do this. Try this:

$text = preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $domDocument->saveHTML()));

这篇关于php DomDocument添加额外的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆