【发布时间】:2011-06-15 14:05:49
【问题描述】:
我正在尝试解析文档并获取所有图像标签并更改源以获取不同的内容。
$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 最初看起来像这样:
<p>Hi, this is a test, here is an image<img src="http://example.com/beer.jpg" width="60" height="95" /> Because I like Beer!</p>
这是$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、正文和顶部的评论)。有什么方法可以设置DOMDocument 以避免添加这些额外的标签?
【问题讨论】:
标签: php dom domdocument