2012-10-28 46 views
2

我似乎有一个问题替换文本链接与网站发布的链接,它不链接。替换文本链接作为链接preg_replace

代码:

$status_text = preg_replace('#(\A|[^=\]\'"a-zA-Z0-9])(http[s]?://(.+?)/[^()<>\s]+)#i', '\\1<a href="\\2">\\3</a>', $status_text); 
echo $status_text; 

$status_text是命名为contents一个MySQL场拉,并含有其他文字,但我只是想linkify的链接。 此外,我也想它不显示完整的网址,只是主要的域名。

UPDATE:我们有两个preg_replaces在同一页上,寻找的东西,在他们面前+和#链接到,他们目前的工作,需要不与上述冲突的网站的区域:

$status_text = preg_replace("/#([a-z_0-9]+)/i", "<a href=\"http://url.com/hashlink/$1\">$0</a>", $status_text); 


$status_text = preg_replace("/\+([a-z_0-9]+)/i", "<a href=\"http://url.com/pluslink/$1\">$0</a>", $status_text); 
+0

你可以给你正在使用的文本的例子吗? – mason81

+0

@ mason81当然,它的一个简单的文本行,例如: hello http:// hello。 com(在这里避免自动链接的空格) – mobile

+0

当你说你想要“不显示完整的URL,只是主要的域名”你指的是链接的href或链接的标签? – mason81

回答

6

这里,试试这个:

$status_text = preg_replace('|([\w\d]*)\s?(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '$1 <a href="$2">$3</a>', $status_text); 
echo $status_text; 

或使其easer一点点阅读:

$m = '|([\w\d]*)\s?(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i'; 
$r = '$1 <a href="$2">$3</a>'; 

$status_text = preg_replace($m,$r,$status_text); 
echo $status_text; 

编辑 - 更新由于OP新的信息 -

你的#标签的正则表达式没有考虑到在URL中考虑散列,所以我们进行了修复... 此外,你应该匹配之前匹配的网址哈希标签或加标签,因为否则就会乱了你的哈希标签创建链接并加标签

$status_text = preg_replace('|(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '<a href="$1">$2</a>', $status_text); 
$status_text = preg_replace('|\B#([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text); 
$status_text = preg_replace('|\B\+([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text); 

还是要使它更容易一点阅读...

$match_href = '|(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i'; 
$match_hash = '|\B#([\d\w_]+)|i'; 
$match_plus = '|\B\+([\d\w_]+)|i'; 
$replace_url = '<a href="$1">$2</a>'; 
$replace_tag = '<a href="http://url.com/pluslink/$1">$0</a>'; 

$status_text = preg_replace($match_href, $replace_url, $status_text); 
$status_text = preg_replace($match_hash, $replace_tag, $status_text); 
$status_text = preg_replace($match_plus, $replace_tag, $status_text); 

再次编辑 - 添加可能会有所帮助的URL -

您可以测试出的正则表达式在这里:http://gskinner.com/RegExr/

ANOTHER编辑

按照评论/问题,如果你想要解释缺少http协议的网址,请使用以下内容:

$status_text = preg_replace('|((https?://)?([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '<a href="$1">$3</a>', $status_text); 
$status_text = preg_replace('|\B#([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text); 
$status_text = preg_replace('|\B\+([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text); 

或者为了使它更容易阅读...

$match_href = '|((https?://)?([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i'; 
$match_hash = '|\B#([\d\w_]+)|i'; 
$match_plus = '|\B\+([\d\w_]+)|i'; 
$replace_url = '<a href="$1">$3</a>'; 
$replace_tag = '<a href="http://url.com/pluslink/$1">$0</a>'; 

$status_text = preg_replace($match_href, $replace_url, $status_text); 
$status_text = preg_replace($match_hash, $replace_tag, $status_text); 
$status_text = preg_replace($match_plus, $replace_tag, $status_text); 

用法示例

输入:(文本从DB - > $ STATUS_TEXT)

<!-- language-all: lang-none --> 
Hi, this is an example. This is a url http://stackoverflow.com/ 
and this is a hash reference that we want to link to an internal 
post #AwesomePost123 and this one is a plus reference we want to 
link to an internal post +AwesomePost123 and finally an example 
of a url without the http protocol www.stackoverflow.com 

输出:

<!-- language-all: lang-none --> 
Hi, this is an example. This is a url <a href="http://stackoverflow.com/">stackoverflow.com</a> 
and this is a hash reference that we want to link to an internal 
post <a href="http://url.com/pluslink/AwesomePost123">#AwesomePost123</a> and this one is a plus reference we want to 
link to an internal post <a href="http://url.com/pluslink/AwesomePost123">+AwesomePost123</a> and finally an example 
of a url without the http protocol <a href="www.stackoverflow.com">www.stackoverflow.com</a> 
+0

我用hello http:// hello试了一下。 com,它的链接,但没有一个空格之前http 我也使用2其他事情,如#hashtags其他preg_reces,它看起来像它被拿出了这些 – mobile

+0

我是不确定你的意思是“它在http之前没有空格”,你能澄清一下吗? – mason81

+0

您可以按照其处理的顺序将完整示例(包括您提到的标签)发布到其他preg_replaces的代码中吗?这会帮助我更好地帮助你。 – mason81

0

(通过正则表达式运行后)能你只是试试这个:

function makeClickableLinks($s) { 
    return preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="$1" target="_blank">$1</a>', $s); 
} 

为了测试它,试试这个:

echo makeClickableLinks('Hello world, you go to www.google.com to check! also http://www.google.com/ works too!'); 

演示在这里:http://codepad.viper-7.com/hkOdCM

+0

我已经更新了其他2个preg_reces我们原来的帖子,它不需要冲突,现在就试试这个,谢谢 编辑:它会导致哈希链接回显:http://url.com/hashlink/ “> #blah – mobile