2012-02-17 79 views
-5

如果键入笨auto_link犯规添加链接加www

www.google.comhttp://www.google.com

使用 auto_link()功能将添加链接是否正确

..

但是,如果键入

google.com 

的功能不起作用,因为www。部分丢失...

我怎样才能确保它将包括这些链接?

这是笨的功能:

/** 
* Auto-linker 
* 
* Automatically links URL and Email addresses. 
* Note: There's a bit of extra code here to deal with 
* URLs or emails that end in a period. We'll strip these 
* off and add them after the link. 
* 
* @access public 
* @param string the string 
* @param string the type: email, url, or both 
* @param bool whether to create pop-up links 
* @return string 
*/ 
if (! function_exists('auto_link')) 
{ 
    function auto_link($str, $type = 'both', $popup = FALSE) 
    { 
     if ($type != 'email') 
     { 
      if (preg_match_all("#(^|\s|\()((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i", $str, $matches)) 
      { 
       $pop = ($popup == TRUE) ? " target=\"_blank\" " : ""; 

       for ($i = 0; $i < count($matches['0']); $i++) 
       { 
        $period = ''; 
        if (preg_match("|\.$|", $matches['6'][$i])) 
        { 
         $period = '.'; 
         $matches['6'][$i] = substr($matches['6'][$i], 0, -1); 
        } 

        $str = str_replace($matches['0'][$i], 
             $matches['1'][$i].'<a href="http'. 
             $matches['4'][$i].'://'. 
             $matches['5'][$i]. 
             $matches['6'][$i].'"'.$pop.'>http'. 
             $matches['4'][$i].'://'. 
             $matches['5'][$i]. 
             $matches['6'][$i].'</a>'. 
             $period, $str); 
       } 
      } 
     } 

     if ($type != 'url') 
     { 
      if (preg_match_all("/([a-zA-Z0-9_\.\-\+]+)@([a-zA-Z0-9\-]+)\.([a-zA-Z0-9\-\.]*)/i", $str, $matches)) 
      { 
       for ($i = 0; $i < count($matches['0']); $i++) 
       { 
        $period = ''; 
        if (preg_match("|\.$|", $matches['3'][$i])) 
        { 
         $period = '.'; 
         $matches['3'][$i] = substr($matches['3'][$i], 0, -1); 
        } 

        $str = str_replace($matches['0'][$i], safe_mailto($matches['1'][$i].'@'.$matches['2'][$i].'.'.$matches['3'][$i]).$period, $str); 
       } 
      } 
     } 

     return $str; 
    } 
} 

SOLUTION: 功能auto_link($海峡,$型= '都',$弹出= FALSE) { 如果($型=! ''email') {!preg_match_all(“/ ^([a-zA-Z0-9 _.-])+ @([a-zA-Z0-9 _.-])+ \。([a- zA-Z])+([a-zA-Z])+ /“,$ str,$ matches)){

 if (preg_match_all("/(?:https?\:?(?:\/\/)?|www\.)?([a-zA-Z0-9\-\.]+\.(?:.[a-z]*))/mi", $str, $matches)) 
     { 
      $pop = ($popup == TRUE) ? " target=\"_blank\" " : ""; 

      for ($i = 0; $i < count($matches['0']); $i++) 
      { 

          $str = str_replace($matches[0][$i], 
               '<a href="http://'.$matches[1][0].'" class="auto_link_color">'.$matches[1][0].'</a>', $str); 
      } 
     } 
      }else{ 
       for ($i = 0; $i < count($matches['0']); $i++) 
       { 
        $str = str_replace($matches[0][$i], $matches[0][0], $str); 
       } 
      } 
    } 

    return $str; 
} 

这解决了我的问题,所以用户输入的任何链接都会找到它并添加链接...即使用户输入电子邮件,也不会在域部分添加链接,而是将其显示为文本。

+2

你的逻辑有瑕疵,试图强制'auto_link()'来做到这一点。它会为''创建一个乱七八糟的',你会最终得到一些不应该包含的文本的随机URL。即使如此,在这里并没有将所有网址格式化为真正的链接那么雄心勃勃。想想看。你会争取一些东西是电子邮件或网址,或者只是一段时间的刺痛。只是不要混淆至少恕我直言,没有被打破的东西。 – Jakub 2012-02-23 03:13:55

回答

0
function auto_link($str, $type = 'both', $popup = FALSE) 
{ 
    if ($type != 'email') 
    { 
      if (!preg_match_all("/^([a-zA-Z0-9_.-])[email protected]([a-zA-Z0-9_.-])+\\.([a-zA-Z])+([a-zA-Z])+/", $str, $matches)){ 

     if (preg_match_all("/(?:https?\:?(?:\/\/)?|www\.)?([a-zA-Z0-9\-\.]+\.(?:.[a-z]*))/mi", $str, $matches)) 
     { 
      $pop = ($popup == TRUE) ? " target=\"_blank\" " : ""; 

      for ($i = 0; $i < count($matches['0']); $i++) 
      { 

          $str = str_replace($matches[0][$i], 
               '<a href="http://'.$matches[1][0].'" class="auto_link_color">'.$matches[1][0].'</a>', $str); 
      } 
     } 
      }else{ 
       for ($i = 0; $i < count($matches['0']); $i++) 
       { 
        $str = str_replace($matches[0][$i], $matches[0][0], $str); 
       } 
      } 
    } 

    return $str; 
} 

这解决了我的问题,所以任何链接的用户进入会发现它和添加链接...即使用户输入电子邮件,也不会在域部分添加链接,而是将其显示为文本。

3

自动链接应正确捕获http://google.com。你是说这是行不通的吗?

自动链接正则表达式使用http(s)或www的存在来指示链接存在。如果没有这些选项,您必须更改正则表达式才能检测.com的顶级域名,由于可能存在大量可能的顶级域名(.net,.org,.biz等) )。如果你认为它通过你可能不想改变这个正则表达式,因为所有可能的域名和新的域名的维护将被添加比它的价值更为繁琐。

+0

我在说,自动链接不会赶上google.com,并要求http:// www。或www。上班。如何修改正则表达式,即使在我的情况下www或http丢失了? – fxuser 2012-02-17 11:57:31

+0

@fxuser你不需要修改正则表达式。只需使用URL助手中的'prep_url()'函数,然后将'auto_link()'函数应用于'prep_url()'返回的值。 – 2012-02-17 12:16:37

+0

事情是,我有一堆文本可能有它的URL ...如果我添加prep_url()之前,然后添加auto_link()它只会在整个文本前面添加http://而不是链接... – fxuser 2012-02-17 13:41:07

1

对不起,但您以错误的方式使用auto_link()。

该参数应该是有效的网址或电子邮件。把刚刚 “www.google.com” 是不允许的,should'nt在所有的工作:-)

所以,你应该做的:

auto_link('http://www.google.com') 
auto_link('http://google.com') 

决不ommitting输入 “http://” 在开始时。

0

如果您不使用“http://”或“www。”作为你的“链接这个”触发器,你必须编写它来捕获.com,.org,.net(以及不断扩展的可能性)。正如其他人所建议的那样,使这个“非常贪婪”的规则可以与不应该是链接的东西相匹配。您可以根据自己的优先权衡量余额。

这里有一个(很简单),小实验我想:

<?php 

header("Content-type: text/plain"); 

$text = 'http://google.com 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed in urna auctor tellus consequat cursus. 
Cras malesuada magna eget felis accumsan vel dictum urna sodales. Vestibulum erat ante, rutrum et 
aliquet vel, convallis eu google.com. Phasellus interdum velit at urna google.com porta. Donec at interdum 
nibh. Fusce ultricies varius elit id egestas. Suspendisse dolor risus, vulputate vel rutrum in, 
http://google.com et nisi. Etiam non massa non neque lacinia adipiscing sed nec metus. Sed fermentum ultricies 
dui at porta. Duis at lacinia tortor. Nam mi est, mollis sed viverra et, mollis ac lorem. In mattis 
lacinia tempor. 

Sed in luctus nunc. Mauris nec tincidunt dui. Vivamus interdum, velit sed lobortis lobortis, nulla dui 
vestibulum dui, eu tincidunt arcu felis et massa. google.COM vitae porta felis. Sed sit amet magna augue. 
Aenean dignissim tempus porta. Donec ultrices lectus ac sapien gravida sodales. Quisque malesuada 
sagittis rhoncus. Vestibulum mattis auctor ligula, eu tempus odio hendrerit in. Ut vel elit ipsum. Sed 
ante lorem, www.google.com et dictum nec, ultricies a lorem. 
'; 

$domains = 'com|org|net'; 

if (!preg_match_all('#([\S]*)(\.('.$domains.']))#i', $text, $matches)) 
{ 
    die('no matches'); 
} 

print_r($matches); 

输出:

Array 
(
    [0] => Array 
     (
      [0] => http://google.com 
      [1] => google.com 
      [2] => google.com 
      [3] => http://google.com 
      [4] => google.COM 
      [5] => www.google.com 
     ) 

    [1] => Array 
     (
      [0] => http://google 
      [1] => google 
      [2] => google 
      [3] => http://google 
      [4] => google 
      [5] => www.google 
     ) 

    [2] => Array 
     (
      [0] => .com 
      [1] => .com 
      [2] => .com 
      [3] => .com 
      [4] => .COM 
      [5] => .com 
     ) 

    [3] => Array 
     (
      [0] => com 
      [1] => com 
      [2] => com 
      [3] => com 
      [4] => COM 
      [5] => com 
     ) 

)