用PHP的DOMDocument更改标签的属性值 [英] Change tag attribute value with PHP DOMDocument
本文介绍了用PHP的DOMDocument更改标签的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想换用PHP DOM文档标签的属性值。
举例来说,假设我们有这一行的HTML:
< A HREF =http://foo.bar/>点击此处< / A>
我加载在PHP上述code如下:
$ DOM =新的DOM文档;
$ dom-> loadHTML('< A HREF =http://foo.bar/>点击此处< / A>');
我想改变的href的值改为http://google.com/使用PHP的DOM文档扩展名。这可能吗?
感谢您的帮助一如既往!
解决方案
$ DOM =新的DOMDocument();
$ dom-> loadHTML('< A HREF =http://foo.bar/>点击此处< / A>');的foreach($ dom->的getElementsByTagName('A')为$项){ $本期特价货品>的setAttribute(HREF','http://google.com/');
回声$ dom-> saveHTML();
出口;
}
I want to change the value of the attribute of a tag with PHP DOMDocument.
For example, say we have this line of HTML:
<a href="http://foo.bar/">Click here</a>
I load the above code in PHP as follows:
$dom = new domDocument;
$dom->loadHTML('<a href="http://foo.bar/">Click here</a>');
I want to change the "href" value to "http://google.com/" using the DOMDocument extension of PHP. Is this possible?
Thanks for the help as always!
解决方案
$dom = new DOMDocument();
$dom->loadHTML('<a href="http://foo.bar/">Click here</a>');
foreach ($dom->getElementsByTagName('a') as $item) {
$item->setAttribute('href', 'http://google.com/');
echo $dom->saveHTML();
exit;
}
这篇关于用PHP的DOMDocument更改标签的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文