PHP的解析链接/电子邮件

问题描述:

我想知道如果有一个简单的片断,其将任何类型的链接:PHP的解析链接/电子邮件

http://www.cnn.com to <a href="http://www.cnn.com">http://www.cnn.com</a> 
cnn.com to <a href="http://www.cnn.com">cnn.com</a> 
www.cnn.com to <a href="http://www.cnn.com">www.cnn.com</a> 
[email protected] to to <a href="mailto:mailto:[email protected]">mailto:[email protected]</a> 

我不希望使用任何PHP5特定库。

谢谢你的时间。

UPDATE我已将上述文本更新为我想将其转换为的内容。请注意,href标记和文字是案件2和3

UPDATE2怎么样了不Gmail聊天做不同?他们非常聪明,只适用于真正的域名。例如a.ly工作但a.cb不起作用。

是, http://www.gidforums.com/t-1816.html

<?php 
/** 
    NAME  : autolink() 
    VERSION  : 1.0 
    AUTHOR  : J de Silva 
    DESCRIPTION : returns VOID; handles converting 
       URLs into clickable links off a string. 
    TYPE  : functions 
    ======================================*/ 

function autolink(&$text, $target='_blank', $nofollow=true) 
{ 
    // grab anything that looks like a URL... 
    $urls = _autolink_find_URLS($text); 
    if(!empty($urls)) // i.e. there were some URLS found in the text 
    { 
    array_walk($urls, '_autolink_create_html_tags', array('target'=>$target, 'nofollow'=>$nofollow)); 
    $text = strtr($text, $urls); 
    } 
} 

function _autolink_find_URLS($text) 
{ 
    // build the patterns 
    $scheme   =  '(http:\/\/|https:\/\/)'; 
    $www   =  'www\.'; 
    $ip    =  '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'; 
    $subdomain  =  '[-a-z0-9_]+\.'; 
    $name   =  '[a-z][-a-z0-9]+\.'; 
    $tld   =  '[a-z]+(\.[a-z]{2,2})?'; 
    $the_rest  =  '\/?[a-z0-9._\/~#&=;%+?-]+[a-z0-9\/#=?]{1,1}';    
    $pattern  =  "$scheme?(?(1)($ip|($subdomain)?$name$tld)|($www$name$tld))$the_rest"; 

    $pattern  =  '/'.$pattern.'/is'; 
    $c    =  preg_match_all($pattern, $text, $m); 
    unset($text, $scheme, $www, $ip, $subdomain, $name, $tld, $the_rest, $pattern); 
    if($c) 
    { 
    return(array_flip($m[0])); 
    } 
    return(array()); 
} 

function _autolink_create_html_tags(&$value, $key, $other=null) 
{ 
    $target = $nofollow = null; 
    if(is_array($other)) 
    { 
    $target  = ($other['target'] ? " target=\"$other[target]\"" : null); 
    // see: http://www.google.com/googleblog/2005/01/preventing-comment-spam.html 
    $nofollow = ($other['nofollow'] ? ' rel="nofollow"'   : null);  
    } 
    $value = "<a href=\"$key\"$target$nofollow>$key</a>"; 
} 

?> 
+0

不工作太好。如果我输入www.google.com,则其链接将保持为www.google.com而不是http://www.google.com。也只有google.com不起作用。 – 2009-06-24 15:01:51

这里的电子邮件片断:

$email = "[email protected]"; 

$pos = strrpos($email, "@"); 
if (!$pos === false) { 
    // This is an email address! 
    $email .= "mailto:" . $email; 
} 

究竟你在找什么用的链接呢?剥离www或http?或在需要时将http://www添加到任何链接?

+0

如果需要添加http:// www然后添加一个href标签给他们 – 2009-06-24 13:35:57

+0

我已更新问题。 – 2009-06-24 13:37:44

尝试了这一点。 (用于链接不支持电子邮件)

$newTweet = preg_replace('!http://([a-zA-Z0-9./-]+[a-zA-Z0-9/-])!i', '<a href="\\0" target="_blank">\\0</a>', $tweet->text); 

我知道5年晚了,但我需要一个类似的解决方案和最佳的答案,我得到的是从用户 - erwan-dupeux-maire

回答

我写这个函数。它会替换字符串中的所有链接。链路可以是以下格式:

第二个参数是该链接的目标(” _blank','_top'...可以设置为false)。希望它有帮助...

public static function makeLinks($str, $target='_blank') 
{ 
    if ($target) 
    { 
     $target = ' target="'.$target.'"'; 
    } 
    else 
    { 
     $target = ''; 
    } 
    // find and replace link 
    $str = preg_replace('@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', '<a href="$1" '.$target.'>$1</a>', $str); 
    // add "http://" if not set 
    $str = preg_replace('/<a\s[^>]*href\s*=\s*"((?!https?:\/\/)[^"]*)"[^>]*>/i', '<a href="http://$1" '.$target.'>', $str); 
    return $str; 
}