Reputation: 5238
$links = $li->getElementsByTagName('a');
foreach ($links as $link)
{
$link_text = $link->nodeValue;
$image = $dom->createElement('img');
$image->setAttribute('src', 'some target');
$image->setAttribute('alt', $link_text);
$link->nodeValue($image); // doesnt work
}
How do I replace link's nodevalue with the new one? (using domdocument)
There is actually one link inside li, but I'm not sure how to get it without foreach.
Upvotes: 0
Views: 223
Reputation: 81988
You could try this (with $doc being your DOMDocument).
// saveHTML returns the node as a string of HTML.
$link->nodeValue = $doc->saveHTML($image);
Or, more appropriately, you could add the image as a child node:
// name should be self-documenting.
$link->appendChild($image);
Also, if you only have one, you could simply use the item
method and avoid the foreach:
$link = $li->getElementsByTagName('a')->item(0);
Upvotes: 1
Reputation: 1441
https://www.php.net/manual/en/class.domnode.php#domnode.props.nodevalue
nodeValue is a string. It cannot be called as a method. You can set the value of this string directly, as it's a public member.
$link->nodeValue = $link_text;
The docs linked above should answer any questions you have.
Upvotes: 0
Reputation: 239260
Have you tried the assignment operator, =
?
$link->nodeValue = $link_text;
Upvotes: 0