im being played by php and DomDocument.... basically i have some html saved in db. With anchor tags with different urls.... i want to force anchor tag hrefs not within allowedur
This should be pretty straight-forward.
First, let's grab all of the anchor tags. $doc
is the document you've created with your HTML as the source.
$anchors = $doc->getElementsByTagName('a');
Now we'll go through them one-by-one and inspect the href
attribute. Let's pretend that the function contains_bad_url
returns true
when the passed string is on your blacklist. You'll need to write that yourself.
foreach($anchors as $anchor)
if($anchor->hasAttribute('href') && contains_bad_url($anchor->getAttribute('href'))) {
$anchor->setAttribute('href', '#');
}
}
Tada. That should be all there is to it. You should be able to get the results back as an XML string and do whatever you need to do with the rest.
Thanx Charles.... came up with this
function contains_bad_urls($href,$allowed_urls)
{
$x=pathinfo($href);
$bn=$x['filename'];
if (array_search($bn,$allowed_urls)>-1)
{
return false;
}
return true;
}
function CleanHtmlUrls($str)
{
$allow_urls = array('viewprofile','viewwall');//change these to whatever filename
$doc = new DOMDocument();
$doc->loadHTML($str);
$doc->formatOutput = true;
$anchors = $doc->getElementsByTagName('a');
foreach($anchors as $anchor)
{
$anchor->setAttribute('onclick','#');
if(contains_bad_urls($anchor->getAttribute('href'),$allow_urls))
{
$anchor->setAttribute('href', '#');
}
}
$ret=$doc->saveHTML();
return $ret
}