【发布时间】:2014-06-11 07:58:33
【问题描述】:
我创建了一个自动检测链接的函数。它工作得很好,除非 HTML 标记在链接之后开始(没有空格)。
例子:
http://www.google.com is cool
变成
<a href="http://www.google.com" target="_blank">www.google.com</a> is cool
但是
http://www.google.com<br />is cool
(实数http://后无空格)变为
<a href="http://www.google.com<br /" target="_blank">www.google.com<br /</a> is cool
我的正则表达式一开始并没有停止<,尽管< 是不允许的...
我该如何解决?或者我怎样才能有效地排除<?
这是我的功能:
function detect_linkg($str){
return preg_replace('#(https?://)([\w\d.&:\#@%/;$~_?\+-=]*)#','<a href="$1$2" target="_blank">$2</a>',$str);
}
感谢您的帮助!
【问题讨论】:
标签: php regex function hyperlink detect