找回密码
 立即注册
查看: 1600|回复: 0

discuz论坛帖子自动给外链添加nofollow属性的方法

[复制链接]

168

主题

1

回帖

375

积分

管理员

积分
375
发表于 2024-11-12 11:10:57 | 显示全部楼层 |阅读模式
修改source/function/function_discucode.php,搜索parseurl函数,修改成以下代码:

  1. function parseurl($url, $text, $scheme) {
  2. global $_G;
  3. if(!$url && preg_match("/((https?|ftp|gopher|news|telnet|rtsp|mms|callto|bctp|thunder|qqdl|synacast){1}:\/\/|www\.)[^\["']+/i", trim($text), $matches)) {
  4. $url = $matches[0];
  5. $length = 65;
  6. if(strlen($url) > $length) {
  7. $text = substr($url, 0, intval($length * 0.5)).' ... '.substr($url, - intval($length * 0.3));
  8. }
  9. $url = nofollow($url);
  10. return '<a href="'.(substr(strtolower($url), 0, 4) == 'www.' ? 'http://'.$url : $url).'"  target="_blank">'.$text.'</a>';
  11. } else {
  12. $url = substr($url, 1);
  13. if(substr(strtolower($url), 0, 4) == 'www.') {
  14. $url = 'http://'.$url;
  15. }
  16. $url = !$scheme ? $_G['siteurl'].$url : $url;
  17. return '<a href="'.nofollow($url).'" target="_blank">'.$text.'</a>';
  18. }
  19. }
复制代码
继续在parseurl函数下面添加代码:
  1. function nofollow($url = '')
  2. {
  3. $temp = array();
  4. if( ! empty($url))
  5. {
  6. $temp = parse_url($url);
  7. if(isset($temp['host']) && $temp['host'] != $_SERVER['HTTP_HOST'])
  8. {
  9. $url .= '"rel="nofollow';
  10. }
  11. }
  12. unset($temp);
  13. return $url;
  14. }
复制代码
至此discuz论坛帖子自动给外链添加nofollow属性就完成了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|洪湖在线网 ( 鄂ICP备18020272号-1 )

GMT+8, 2025-6-4 09:32

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表