質問

$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.

役に立ちましたか?

解決

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);

他のヒント

Have you tried the assignment operator, =?

$link->nodeValue = $link_text;

http://us2.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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top