PHP remove links to specific website but keep text

前端 未结 1 1179
一向
一向 2021-01-27 04:16

For example, remove links to here but keep text but leave all other

相关标签:
1条回答
  • 2021-01-27 05:18
    $html = '...I can haz HTML?...';
    $whitelist = array('herpyderp.com', 'google.com');
    
    $dom = new DomDocument();
    $dom->loadHtml($html);    
    $links = $dom->getELementsByTagName('a');
    
    foreach($links as $link){
      $host = parse_url($link->getAttribute('href'), PHP_URL_HOST);
    
      if($host && !in_array($host, $whitelist)){    
    
        // create a text node with the contents of the blacklisted link
        $text = new DomText($link->nodeValue);
    
        // insert it before the link
        $link->parentNode->insertBefore($text, $link);
    
        // and remove the link
        $link->parentNode->removeChild($link);
      }  
    
    }
    
    // remove wrapping tags added by the parser
    $dom->removeChild($dom->firstChild);            
    $dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild);
    
    $html = $dom->saveHtml();
    

    For those scared to use DomDocument instead of preg_replace for performance reasons, I did a quick test between this and the code linked in the Q (the one that completely removes the links) => DomDocument is only ~4 times slower.

    0 讨论(0)
提交回复
热议问题