简体   繁体   English

用于匹配 php 字符串中的 http 和 www url 的正则表达式

[英]regex for matching http and www urls in a php string

Here is the code i am using这是我正在使用的代码

function parseURL($text) {
    $regex = "#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#iS";
    preg_match_all($regex, $text, $matches);
    foreach($matches[0] as $pattern){
        $text = str_replace($pattern, "<a href=\"$pattern\" target=\"_blank\">$pattern</a> ", $text);   
    }
    return $text;
}

For some reason my regex is outputting the following results: (bold = linked)出于某种原因,我的正则表达式输出以下结果:(粗体 = 链接)

www.domain.com www.domain.com

http:// www.domain.com HTTP:// www.domain.com

http://domain.com http://domain.com

so it works fine except if it contains both http and www at which point it only links from the www part onward.所以它工作正常,除非它同时包含 http 和 www,此时它只从 www 部分开始链接。

any idea why?知道为什么吗?

EDIT编辑

For anyone reading this requiring the fix, here is the working code thanks to Wiktor Stribiżew ..对于任何阅读这篇需要修复的人,这里是工作代码,感谢Wiktor Stribiżew ..

function parseURL($text) {
    $regex = "@\b(([\w-]+://?|www[.])[^\s()<>]+(?:\(\w+\)|([^[:punct:]\s]|/)))@i";
    $subst = "<a href='$0' target='_blank'>$0</a>";
    $text = preg_replace($regex, $subst, $text);
    return $text;
}

You do not need to first collect matches and then replace each one by one.您无需先收集匹配项,然后再逐个替换。 Use a preg_replace directly and use a $0 backreference to refer to the whole match from the replacement pattern.直接使用preg_replace并使用$0反向引用来引用替换模式中的整个匹配项。

See the PHP demo :请参阅PHP 演示

$re = '@\b(([\w-]+://?|www[.])[^\s()<>]+(?:\(\w+\)|([^[:punct:]\s]|/)))@i';
$str = "www.domain.com\nhttp://www.domain.com\nhttp://domain.com";
$subst = '<a href="$0" target="_blank">$0</a> ';
$result = preg_replace($re, $subst, $str);
echo $result;

Output:输出:

<a href="www.domain.com" target="_blank">www.domain.com</a> 
<a href="http://www.domain.com" target="_blank">http://www.domain.com</a> 
<a href="http://domain.com" target="_blank">http://domain.com</a> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM