php使用preg_replace将URL转换为链接。。。。。有时P

php使用preg_replace将URL转换为链接。。。。。有时P,php,regex,preg-replace,Php,Regex,Preg Replace,可能重复: 好的,所以我使用OsTicket(电子邮件票证系统),并将其转换为使用HTML格式的电子邮件进出。它被黑客攻击了,但它能工作 现在有一个函数叫做“ClickableURL” 函数可点击URL($text){ //虽然不完美,但很有效-请帮助改进它。 $text=preg\u replace('/([^(\'''.'))((f|ht){1}tp(s?):\/\/)[-a-zA-Z0-9@:%\+.~\'.~(35;?&;\/\/=]+[^(\'.\''.')/”,'$text);

可能重复:

好的,所以我使用OsTicket(电子邮件票证系统),并将其转换为使用HTML格式的电子邮件进出。它被黑客攻击了,但它能工作

现在有一个函数叫做“ClickableURL”

函数可点击URL($text){
//虽然不完美,但很有效-请帮助改进它。
$text=preg\u replace('/([^(\'''.'))((f|ht){1}tp(s?):\/\/)[-a-zA-Z0-9@:%\+.~\'.~(35;?&;\/\/=]+[^(\'.\''.')/”,'$text);
$text=preg\u replace(“/(^('\\\\\\”)([^('\\\”)www\.([a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]+)(\/[^\/\\n\\r]*)*[^('\\\\”)/“,
“\\1”,$text);
$text=preg\u replace(“/(^('.\\\”)([^('.\\”)[\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\)+[a-z]{2,4}[^('.\\”)/”,'\\1',$text);
返回$text;
}
正如您所看到的,它基本上需要一个URL(搜索http、https或www),然后在其中添加适当的含糊不清的内容

我现在有一个WYSIWYG设置。。。。因此,如果我们的一名技术人员使用所见即所得来创建链接,则会自动创建代码。。。。然后clickableurls函数还会找到文本中已经存在的http并再次转换它

所以我基本上是想找出处理这个问题的最佳方法。两种选择我都想要。。我在想也许做一个IF语句,但是改变preg_替换不是更有意义吗


有什么想法吗?再次感谢社区

我可以尝试帮助您翻译第一个preg_replace()正则表达式中的内容。
这是我能做的最好的了,因为我不知道你的意图。我推断出了一些意图,
但这要由你来决定

当正则表达式很复杂时,块格式有助于轻松看穿复杂性

([^(\''\'))((f|ht){1}tp(s?):\/\/)[-a-zA-Z0-9:%\+.~\'?&;\/\/=]+[^(\''\'))


你的正则表达式有问题。在不知道具体细节的情况下,很难帮助你。搜索“LinkifyURL”-这里有很多答案!().
  function clickableurls($text) {

        //Not perfect but it works - please help improve it. 
        $text=preg_replace('/([^(\'|")]((f|ht){1}tp(s?):\/\/)[-a-zA-Z0-9@:%_\+.~#?&;\/\/=]+[^(\'|")])/','<a href="\\1" target="_blank">\\1</a>', $text);
        $text=preg_replace("/(^|[ \\n\\r\\t])([^('|\")]www\.([a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+)(\/[^\/ \\n\\r]*)*[^('|\")])/",
                '\\1<a href="http://\\2" target="_blank">\\2</a>', $text);
        $text=preg_replace("/(^|[ \\n\\r\\t])([^('|\")][_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}[^('|\")])/",'\\1<a href="mailto:\\2" target="_blank">\\2</a>', $text);


        return $text;
    }
(                                    # Capture group 1
    [^'"]                                # Consume a character that is not single nor double quote
    (                                    # Capture group 2
      (?:ftp|https?):                       # Either ftp, OR http (optional s) string literal
      //                                    # Literl double forward shashes
    )                                    # End capt grp 2
    [-a-zA-Z0-9@:%_+.~#?&;/=]+           # 1 or more (greedy) of the characters in this class
    [^'"]                                # Consume a character that is not single nor double quote
)                                    # End capt grp 1