N
N
Nikolai Vasin2020-05-15 15:33:43
PHP
Nikolai Vasin, 2020-05-15 15:33:43

How to add an attribute with a value to a tag using the DOMDocument class?

Tried using setAttribute method - doesn't help

$dom->loadHtml($html);
$h2s = $dom->getElementsByTagName('h2');
foreach ($h2s as $h2) {
    $h2->setAttribute('id', 5);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Gordinskiy, 2020-05-15
@DmitriyGordinskiy

Where do you expect to see it?
The method adds an attribute to the object, from which you then need to build html again.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question