How to set element id using php dom?

I want to set the id of an element. I am using php dom. I could not understand how the work would follow.

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

The only description I found for this in the manual is: Declares an attribute name of type ID.

How can i do this?

+3
source share
3 answers

If you want to add the id attribute to your html element so that it <p id="frob">...does not look like you are using it setIdAttribute(), it declares that the attribute $namecan be used as a unique identifier for this element - as an alternative / adding attribute id. Use setAttribute()as follows:

$dom = new DOMDocument();
$dom->loadHTML('<html><body><p>FROB</p></body></html>');
$dom->getElementsByTagName('p')->item(0)->setAttribute('id', 'XXX');
print $dom->saveHTML();
+1
source

, :

DOMElement->setIdAttribute  ('myid', true  );
+4

DOMElement::setAttribute? setAttribute('id', 'your_id'), - ? setIdAttribute('id', TRUE), .

0

Source: https://habr.com/ru/post/1762645/


All Articles