PHP Getting and Setting tag attributes
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 allowedurl list to be replaced with #
eg
$allowed_url_basenames = array('viewprofile.php','viewalbum.php');
sample content from db1
<table cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top">
<a href="viewprofile.php?userid=780">Edrine Kasasa </a> has
</td>
<td valign="top">
invited 10 friend(s) to veepiz using the <a href="invite.php">Invite Tool</a>
</td>
</tr>
</tbody>
i want a php function that will leave first anchor tag href intact and change the se开发者_开发技巧cond to href='#'.
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
}
精彩评论