PHP Getting and Setting tag attributes

前端 未结 2 1302
天涯浪人
天涯浪人 2021-01-28 15:50

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

相关标签:
2条回答
  • 2021-01-28 16:19

    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.

    0 讨论(0)
  • 2021-01-28 16:33

    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
    }   
    
    0 讨论(0)
提交回复
热议问题