Im using a PHP foreach loop to walk through an array of hyperlinks. if a condition is met, i want to change the href of the hyperlink. if not, the loop can continue. I am able to echo the current href using: echo $node->getAttribute( 'href' ); However i'm not able to change it using: $node->setAttribute('href', "https://ift.tt/2TjGdB8);
Im missing something here, but i have been stuck on it for a while.
Full code:
$dom = new DOMDocument;
$dom->loadHTML($homepage);
foreach ($dom->getElementsByTagName('a') as $node) {
if ($node->getAttribute( 'href' ) == "https://www.website1.com" ) {
echo $node->getAttribute( 'href' );
$node->setAttribute('href', "https://www.website2.com");
}
}
$html = $dom->saveHTML();
source https://stackoverflow.com/questions/67938720/php-change-href-when-condition-is-met
No comments:
Post a Comment