简体   繁体   English

仅在域存在时将链接转换为锚

[英]Convert link to anchor only if domain exists

I have a link detection in my site which turns links input by users to anchors but I want to limit conversion of crap links that don't exist and I have built the following 我的网站上有一个链接检测功能,它将用户输入的链接转换为锚点,但是我想限制不存在的废话链接的转换,因此我建立了以下链接

public function tLink($s){
    $domain = preg_replace('/(http|ftp)+(s)?:(\/\/)((\w|\.)+)(\/)?(\S+)?/i', '\4', $s);
    getmxrr($domain,$result);
    if(!empty($result)){
        return preg_replace('/(http|ftp)+(s)?:(\/\/)((\w|\.)+)(\/)?(\S+)?/i', '<a href="\0" title="\0">\4</a>', $s);
    }
    return $s;
}

But this makes my pages load really slow, takes anywhere from 2 to 5 seconds to load. 但这使我的页面加载速度非常慢,加载时间从2到5秒不等。 ( they used to load instantly ). (他们过去经常立即加载)。 Is there a better method I can use? 我可以使用更好的方法吗?

You can cache the output of getmxrr to ensure your not doing the same operation for already validated domains. 您可以缓存getmxrr的输出,以确保对已验证的域不执行相同的操作。

assuming you have configured and install memcache. 假设您已经配置并安装了内存缓存。 You can replace your lookup with this function 您可以使用此功能替换查找

function domain_found($domain) { 
$memcache_obj = new Memcache;
$memcache_obj->connect('localhost', 11211);
$var = $memcache_obj->get($domain);
if ($var == "found") return true;
if ($var == "notfound") return false;

    getmxrr($domain,$result); 
    if (empty($result)) {
      $memcache_obj->put($domain, 'notfound');
      return false;
    } else {
      $memcache_obj->put($domain, 'found');
      return true;
    }   
}

You can ofcourse work this inside a class if you like, and optimize other value elements. 当然,您可以根据需要在类中进行此操作,并优化其他值元素。 This serves as a proof of concept code. 这用作概念证明代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM