0

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 &nbsp;
        </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 second to href='#'.

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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
}   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.