【问题标题】:Automatic link detector (PHP + RegEx)自动链接检测器(PHP + RegEx)
【发布时间】: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


    【解决方案1】:

    我认为问题出在:+-=

    这实际上创建了一个范围,例如a-z,但在本例中是从+=,如果您查看ascii 图表,您会看到其中包括&lt;(但不包括@987654327 @)。

    如果您想要文字,只需转义 -+\-=

    【讨论】:

    • 谢谢,这就是解决方案!
    猜你喜欢
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 2018-12-02
    相关资源
    最近更新 更多