我找到了这段代码: 但2链接没有正确。 将我的代码保存在php文件中进行测试。
$a = <<<_END
www.coders.com/coder6602.html => Work
www.avan.ir?a=21 => Work
www.limited.ndot.in/kansas/#?w=500 => Not Work
http://www.sales.com => Work
http://www.mediafire.com/?298nlpla3eys9g6 => Work
http://diary.com/files/draft.html و http://www.logo.net/plogo.swf => Not Work (this is two link)
_END;
/*
<a href="http://limited.ndot.in/kansas/">limited.ndot.in/kansas/</a>#?w=500
<a href="http://diary.com/files/draft.html و logo.net/plogo.swf">diary.com/files/draft.html و logo.net/plogo.swf</a>
*/
function strHyperlink($str){
$pattern_url = '~(?>[a-z+]{2,}://|www\.)(?:[a-z0-9]+(?:\.[a-z0-9]+)?@)?(?:(?:[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])(?:\.[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])+|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?:/[^\\/:?*"<>|\n]*[a-z0-9])*/?(?:\?[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?(?:&[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?)*)?(?:#[a-z0-9_%.]+)?~i';
$str = preg_replace('/http:\/\//','www.', $str);
$str = preg_replace('/www.www./','www.', $str);
$str = preg_replace($pattern_url,"<a href=\"http://\\0\">\\0</a>", $str);
return preg_replace('/www./','',$str);
}
echo strHyperlink($a);
答案 0 :(得分:2)
使用此正则表达式:\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))
您可以在http://gskinner.com/RegExr/
上进行测试我刚用你的网址测试了这个正则表达式并且运行得很好。