Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
自动链接检测器(PHP+;RegEx)_Php_Regex_Function_Hyperlink_Detect - Fatal编程技术网

自动链接检测器(PHP+;RegEx)

自动链接检测器(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后没有空格)变为 我的正则表达式一开始并没有停止我想问题在这里:+-= 这实际上创建了

我创建了一个自动检测链接的函数。它可以很好地工作,除非HTML标记在链接之后开始(没有空格)

例如:

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);
}