PHP删除指向特定网站的链接但保留文本 [英] PHP remove links to specific website but keep text

查看:109
本文介绍了PHP删除指向特定网站的链接但保留文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,< a href =http://msdn.microsoft.com/art029nr/>删除此处的链接,但保留文字< / a>但< a href =http://herpyderp.com>单独保留所有其他链接< / a>



我'我一直试图用preg_replace解决这个问题。我在这里搜索并找到了解决问题的答案。



答案 PHP:从文本中删除特定域的所有超链接删除指向特定URL的链接,但也删除文本。



http://php-opensource-help.blogspot.ie/2010/10/how-to-remove-hyperlink-from-string.html 从字符串中删除超链接但是我似乎无法修改模式,因此它只适用于特定的网站。

解决方案

  $ html ='...我可以隐藏HTML?...'; 
$ whitelist = array('herpyderp.com','google.com');

$ dom = new DomDocument();
$ dom-> loadHtml($ html);
$ links = $ dom-> getELementsByTagName('a');

foreach($ link as $ link){
$ host = parse_url($ link-> getAttribute('href'),PHP_URL_HOST);

if($ host&&!in_array($ host,$ whitelist)){

//使用列入黑名单的链接$ b的内容创建一个文本节点$ b $ text = new DomText($ link-> nodeValue);

//在链接前插入
$ link-> parentNode-> insertBefore($ text,$ link);

//并删除链接
$ link-> parentNode-> removeChild($ link);
}

}

//删除解析器添加的包装标签
$ dom-> removeChild($ dom-> firstChild);
$ dom-> replaceChild($ dom-> firstChild-> firstChild-> firstChild,$ dom-> firstChild);

$ html = $ dom-> saveHtml();






对于那些害怕使用DomDocument而不是<$的人c $ c> preg_replace 出于性能原因,我在这个和Q中链接的代码之间进行了快速测试(完全删除了链接的代码)=> DomDocument只慢了~4倍。 / p>

For example, <a href="http://msdn.microsoft.com/art029nr/">remove links to here but keep text</a> but <a href="http://herpyderp.com">leave all other links alone</a>

I've been trying to solve this using preg_replace. I've searched through here and found answers that solve pieces of the problem.

The answer at PHP: Remove all hyperlinks of specific domain from text removes links to a specific url but removes the text also.

The site at http://php-opensource-help.blogspot.ie/2010/10/how-to-remove-hyperlink-from-string.html removes a hyperlink from a string but I can't seem to modify the pattern so that it applies only to a specific website.

解决方案

$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.

这篇关于PHP删除指向特定网站的链接但保留文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆