Page is a not externally linkable
brotherhood_of_LAN - 1:04 pm on Jul 4, 2011 (gmt 0)
Marked, you can manipulate the HTML as well. Hopefully this example is clear enough. If "getattribute" is matched in the link, it's going to switch the link. You can use the first example to echo out the new values, this example will show you the original document with the altered HTML nodes.
<?php
$htmlstring = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<a href="http://www.php.net/manual/en/domelement.getattribute.php">getattribute()</a>
</body></html>';
$dom = new DOMDocument;
$dom->loadHTML($htmlstring);
foreach($dom->getElementsByTagName('a') as $link)
{
$href = $link->getAttribute('href');
if(preg_match("/getattribute/",$href))
{
$link->setAttribute('href','http://www.php.net/manual/en/domelement.setattribute.php'); // Change href attribute
$link->nodeValue = 'setAttribute()'; // Change 'inner HTML'
}
}
echo $dom->saveHTML();
?>
There are some good examples lurking about online but as I say, I found the manual a touch confusing but otherwise the functions are extremely useful.