how to set id of an element using PHP dom?
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 manu开发者_如何学编程al is - Declares the attribute name to be of type ID.
How can I do this ?
use for example:
DOMElement->setIdAttribute ('myid', true );
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();
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.
精彩评论