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 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改