how to set id of an element using PHP dom?

2019-06-28 03:27发布

I want to set id of an element. I'm using php dom. I could not understand how following will work.

DOMElement::setIdAttribute  ( string $name  , bool $isId  )

The only description I found for this in manual is - Declares the attribute name to be of type ID.

How can I do this ?

标签: php xml parsing
3条回答
2楼-- · 2019-06-28 03:59

What about DOMElement::setAttribute? You could just setAttribute('id', 'your_id'), or am I missing something? After that, you could use setIdAttribute('id', TRUE) to mark it as the ID.

查看更多
该账号已被封号
3楼-- · 2019-06-28 04:19

If you want to add an id attribute to your html element so that it looks like <p id="frob">... you dont use setIdAttribute() - it declares that the attribute $name can be used as an unique identifier for that element - as an alternative/addition of the id attribute. Use setAttribute() like so:

$dom = new DOMDocument();
$dom->loadHTML('<html><body><p>FROB</p></body></html>');
$dom->getElementsByTagName('p')->item(0)->setAttribute('id', 'XXX');
print $dom->saveHTML();
查看更多
太酷不给撩
4楼-- · 2019-06-28 04:22

use for example:

DOMElement->setIdAttribute  ('myid', true  );
查看更多
登录 后发表回答