PHP / RegEx - 通过检测.com / .net / .org / .edu等将URL转换为链接 [英] PHP / RegEx - Convert URLs to links by detecting .com/.net/.org/.edu etc

查看:93
本文介绍了PHP / RegEx - 通过检测.com / .net / .org / .edu等将URL转换为链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有许多问题要求帮助将URL转换为字符串中的可点击链接,但我还没有找到我想要的内容。

I know there have been many questions asking for help converting URLs to clickable links in strings, but I haven't found quite what I'm looking for.

我希望能够匹配以下任何示例并将其转换为可点击的链接:

I want to be able to match any of the following examples and turn them into clickable links:

http://www.domain.com
https://www.domain.net
http://subdomain.domain.org
www.domain.com/folder
subdomain.domain.net
subdomain.domain.edu/folder/subfolder
domain.net
domain.com/folder

我不想匹配random.stuff.separated.with.periods。

I do not want to match random.stuff.separated.with.periods.

编辑:请记住,需要找到这些网址在更大的正常文本字符串中。例如,我想匹配Hello!来检查域名网络!中的domain.net。

Please keep in mind that these URLs need to be found within larger strings of 'normal' text. For example, I want to match 'domain.net' in "Hello! Come check out domain.net!".

我认为这可以通过正则表达式完成可以确定匹配的URL是否包含.com,.net,.org或.edu,后跟正斜杠或空格。除了用户输入错误之外,我无法想象任何其他情况,其中有效的URL会有其中一个跟随其他任何内容。

I think this could be accomplished with a regex that can determine whether the matching url contains .com, .net, .org, or .edu followed by either a forward slash or whitespace. Other than a user typo, I can't imagine any other case in which a valid URL would have one of those followed by anything else.

我意识到有很多有效的域扩展,但我不需要全部支持它们。我可以在正则表达式中选择支持(com | net | org | edu)之类的内容。不幸的是,我对正则表达式还不够熟练,但还不知道如何正确实现它。

I realize there are many valid domain extensions out there, but I don't need to support them all. I can just choose which to support with something like (com|net|org|edu) in the regex. Unfortunately, I'm not skilled enough with regex yet to know how to properly implement this.

我希望有人可以帮我找到正则表达式(用于PHP的preg_replace)可以匹配基于几个任何由一个或多个点连接的文本的URL,并以一个指定的扩展名结尾,后跟空格或包含一个指定的扩展名,后跟一个斜杠和可能的文件夹。

I'm hoping someone can help me find a regular expression (for use with PHP's preg_replace) that can match URLs based on just about any text connected by one or more dots and either ending with one of the specified extensions followed by whitespace OR containing one of the specified extensions followed by a slash and possibly folders.

我做了几次搜索,到目前为止还没找到我要找的东西。如果已经有一个SO帖子回答这个,我道歉。

I did several searches and so far have not found what I'm looking for. If there already exists a SO post that answers this, I apologize.

提前致谢。

---编辑3 ---

--- EDIT 3 ---

经过几天的试验和错误以及来自SO的一些帮助,这是有效的:

After days of trial and error and some help from SO, here's what works:

preg_replace_callback('#(\s|^)((https?://)?(\w|-)+(\.(\w+|-)*)+(?<=\.net|org|edu|com|cc|br|jp|dk|gs|de)(\:[0-9]+)?(?:/[^\s]*)?)(?=\s|\b)#is',
                create_function('$m', 'if (!preg_match("#^(https?://)#", $m[2]))
                return $m[1]."<a href=\"http://".$m[2]."\">".$m[2]."</a>"; else return $m[1]."<a href=\"".$m[2]."\">".$m[2]."</a>";'),
                $event_desc);

这是下面anubhava代码的修改版本,到目前为止似乎完全符合我的要求。谢谢!

This is a modified version of anubhava's code below and so far seems to do exactly what I want. Thanks!

推荐答案

你可以使用这个正则表达式:

You can use this regex:

#(\s|^)((?:https?://)?\w+(?:\.\w+)+(?<=\.(net|org|edu|com))(?:/[^\s]*|))(?=\s|\b)#is

代码:

$arr = array(
'http://www.domain.com/?foo=bar',
'http://www.that"sallfolks.com',
'This is really cool site: https://www.domain.net/ isn\'t it?',
'http://subdomain.domain.org',
'www.domain.com/folder',
'Hello! You can visit vertigofx.com/mysite/rocks for some awesome pictures, or just go to vertigofx.com by itself',
'subdomain.domain.net',
'subdomain.domain.edu/folder/subfolder',
'Hello! Check out my site at domain.net!',
'welcome.to.computers',
'Hello.Come visit oursite.com!',
'foo.bar',
'domain.com/folder',

);
foreach($arr as $url) {   
   $link = preg_replace_callback('#(\s|^)((?:https?://)?\w+(?:\.\w+)+(?<=\.(net|org|edu|com))(?:/[^\s]*|))(?=\s|\b)#is',
           create_function('$m', 'if (!preg_match("#^(https?://)#", $m[2]))
               return $m[1]."<a href=\"http://".$m[2]."\">".$m[2]."</a>"; else return $m[1]."<a href=\"".$m[2]."\">".$m[2]."</a>";'),
           $url);
   echo $link . "\n";

输出:

<a href="http://www.domain.com/?foo=bar">http://www.domain.com/?foo=bar</a>
http://www.that"sallfolks.com
This is really cool site: <a href="https://www.domain.net">https://www.domain.net</a>/ isn't it?
<a href="http://subdomain.domain.org">http://subdomain.domain.org</a>
<a href="http://www.domain.com/folder">www.domain.com/folder</a>
Hello! You can visit <a href="http://vertigofx.com/mysite/rocks">vertigofx.com/mysite/rocks</a> for some awesome pictures, or just go to <a href="http://vertigofx.com">vertigofx.com</a> by itself
<a href="http://subdomain.domain.net">subdomain.domain.net</a>
<a href="http://subdomain.domain.edu/folder/subfolder">subdomain.domain.edu/folder/subfolder</a>
Hello! Check out my site at <a href="http://domain.net">domain.net</a>!
welcome.to.computers
Hello.Come visit <a href="http://oursite.com">oursite.com</a>!
foo.bar
<a href="http://domain.com/folder">domain.com/folder</a>

PS:此正则表达式仅支持URL中的http和https方案。例如:如果你想支持ftp,那么你需要稍微修改一下正则表达式。

PS: This regex only supports http and https scheme in URL. So eg: if you want to support ftp also then you need to modify the regex a little.

这篇关于PHP / RegEx - 通过检测.com / .net / .org / .edu等将URL转换为链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆