dpqvwgr74759 2016-11-21 22:17
浏览 214
已采纳

修改函数以将文本URL转换为链接并限制名称

So I have this function to convert text URLs to links. What do I need to add to preg_replace, to limit a long URL, currently it displays domain only, I would like to add a 4th attribute to this, that shows limit from full URL, limits the URL, I have no idea how to do this. This function is tested and works very well this is why I want to keep using it, how to I add this extra snippet for 4th attribute.

function autolink($str, $attributes=array()) {
    $attrs = '';
    foreach ($attributes as $attribute => $value) {
        $attrs .= " {$attribute}=\"{$value}\"";
    }
    $str = ' ' . $str;
    $str = preg_replace(
        '|([\w\d]*)\s(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i',
        '$1 <a href="$2"'.$attrs.' title="$2" target="_blank" rel="nofollow">$3</a>',
        $str
    );
    return $str;
}

Can someone please help me out.

INPUT:

$cont=autolink("https://www.example.com/test.php?ss=a https://www.example.com/2 https://www.exxxxxxxxxxxxxxxxxxaaample.com/3");

Output current:

   <a href="https://www.example.com/test.php?ss=a" title="https://www.example.com/test.php?ss=a" target="_blank" rel="nofollow">www.example.com</a> <a href="https://www.example.com/2" title="https://www.example.com/2" target="_blank" rel="nofollow">www.example.com</a> <a href="https://www.exxxxxxxxxxxxxxxxxxaaample.com/3" title="https://www.exxxxxxxxxxxxxxxxxxaaample.com/3" target="_blank" rel="nofollow">www.exxxxxxxxxxxxxxxxxxaaample.com</a>

WANTED output:

   <a href="https://www.example.com/test.php?ss=a" title="https://www.example.com/test.php?ss=a" target="_blank" rel="nofollow">www.example.com/test...</a> <a href="https://www.example.com/2" title="https://www.example.com/2" target="_blank" rel="nofollow">www.example.com/2</a> <a href="https://www.exxxxxxxxxxxxxxxxxxaaample.com/3" title="https://www.exxxxxxxxxxxxxxxxxxaaample.com/3" target="_blank" rel="nofollow">www.exxxxxxxxxxxxx.....</a>

I should be able to limit the URL name to like 40 chars. Anyways the third attribute is not needed and would be modified, not returning the domain, but the real name of URL limited to 40 chars.

  • 写回答

1条回答 默认 最新

  • dongpo1599 2016-11-22 00:49
    关注

    I hope it's final: answer is *preg_replace_callback*and this is the best regex, used here, best way to make text URL to HTML link, easy to modify.

    function autolink($textOriginal)
        {
            $textOriginal=" $textOriginal";
            $urlPattern = '|([\w]*)\s(https?://([\w.-]+\.[\w.]{2,6})[^\s\]\[\<\>]*/?)|i';
            $textNew    = preg_replace_callback($urlPattern, "replaceLinkParts", $textOriginal);
    
            return $textNew;
        }
    
        function replaceLinkParts($matches)
        {
     $thefullurl=''.$matches[2].'';
      $whatisthis=''.$matches[1].'';
    
     //////////
        $myDomainx = parse_url("$thefullurl");
         $simpleurl="" . $myDomainx["host"] . "";
             $simpleurlp = "" . $myDomainx["path"] . "";
             if ($simpleurlp == "/") { 
              $simpleurlp = "";
              }
             $newurl="$simpleurl$simpleurlp";
             ////////////////
             $lastpart= "";
    
             $limichars="30";        
    $limitlast="10";
    $calcit=$limichars-$limitlast;
    $nmre=strlen($newurl);
    if ($nmre > $limichars) { 
    $lastpart=$nmre - $limitlast;
    $lastpart= substr($newurl, $lastpart, $limitlast);
    
    $newurl= substr($newurl, 0, $calcit);
    $newurl= ''.$newurl.'...';
      } 
      ////////////
      $retthis=''.$whatisthis.' <a target="_blank" title="'.$thefullurl.'" rel="nofollow" href="'.$thefullurl.'">'.$newurl.''.$lastpart.'</a>';
    
            return $retthis;
        }
    

    Outputs HTML link: www.example.com...ifverylongurl

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行