自动链接检测器(PHP+;RegEx)
我创建了一个自动检测链接的函数。它可以很好地工作,除非HTML标记在链接之后开始(没有空格) 例如:自动链接检测器(PHP+;RegEx),php,regex,function,hyperlink,detect,Php,Regex,Function,Hyperlink,Detect,我创建了一个自动检测链接的函数。它可以很好地工作,除非HTML标记在链接之后开始(没有空格) 例如: http://www.google.com is cool 变成 <a href="http://www.google.com" target="_blank">www.google.com</a> is cool 但是 (http://in real后没有空格)变为 我的正则表达式一开始并没有停止我想问题在这里:+-= 这实际上创建了
http://www.google.com is cool
变成
<a href="http://www.google.com" target="_blank">www.google.com</a> is cool
但是
(http://in real后没有空格)变为
我的正则表达式一开始并没有停止
我想问题在这里:+-=
这实际上创建了一个范围,如a-z
,但在本例中是从+
到=
,如果您查看图表,您会看到其中包括
(但不是code>
)
如果您需要文本,只需转义
-
:+\-=
谢谢,这就是解决方案!
http://www.google.com<br />is cool
<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);
}