2012-02-17 76 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 
     ) 

)