php 正则表达式模式以匹配带有或不带有 http://www 的 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6427530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:21:20  来源:igfitidea点击:

Regular expression pattern to match url with or without http://www

phpregex

提问by Edmund Rojas

I'm not very good at regular expressions at all.

我根本不擅长正则表达式。

I've been using a lot of framework code to date, but I'm unable to find one that is able to match a URL like http://www.example.com/etcetcbut also is able to catch something like www.example.com/etcetcand example.com/etcetc.

迄今为止,我一直在使用很多框架代码,但我无法找到一个能够匹配 URL 之类的代码,http://www.example.com/etcetc但也能够捕获诸如www.example.com/etcetc和 之类的东西example.com/etcetc

Any help would be great. Thanks guys!

任何帮助都会很棒。谢谢你们!

回答by anubhava

For matching all kind of URLs following code should work:

为了匹配所有类型的 URL,以下代码应该可以工作:

<?php
    $regex = "((https?|ftp)://)?"; // SCHEME
    $regex .= "([a-z0-9+!*(),;?&=$_.-]+(:[a-z0-9+!*(),;?&=$_.-]+)?@)?"; // User and Pass
    $regex .= "([a-z0-9\-\.]*)\.(([a-z]{2,4})|([0-9]{1,3}\.([0-9]{1,3})\.([0-9]{1,3})))"; // Host or IP
    $regex .= "(:[0-9]{2,5})?"; // Port
    $regex .= "(/([a-z0-9+$_%-]\.?)+)*/?"; // Path
    $regex .= "(\?[a-z+&$_.-][a-z0-9;:@&%=+/$_.-]*)?"; // GET Query
    $regex .= "(#[a-z_.-][a-z0-9+$%_.-]*)?"; // Anchor
?>

Then, the correct way to check against the regex is as follows:

然后,检查正则表达式的正确方法如下:

<?php
   if(preg_match("~^$regex$~i", 'www.example.com/etcetc', $m))
      var_dump($m);

   if(preg_match("~^$regex$~i", 'http://www.example.com/etcetc', $m))
      var_dump($m);
?>

Courtesy:Comments made by splattermaniaon PHP manual: http://php.net/manual/en/function.preg-match.php

Courtesy:splattermania对 PHP 手册的评论:http: //php.net/manual/en/function.preg-match.php

RegEx Demo in regex101

regex101 中的正则表达式演示

回答by H A?d?μ

This works for me in all cases I had tested:

这适用于我测试过的所有情况:

$url_pattern = '/((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z0-9\&\.\/\?\:@\-_=#])*/';

Tests:

测试:

http://test.test-75.1474.stackoverflow.com/
https://www.stackoverflow.com
https://www.stackoverflow.com/
http://wwww.stackoverflow.com/
http://wwww.stackoverflow.com


http://test.test-75.1474.stackoverflow.com/
http://www.stackoverflow.com
http://www.stackoverflow.com/
stackoverflow.com/
stackoverflow.com

http://www.example.com/etcetc
www.example.com/etcetc
example.com/etcetc
user:[email protected]/etcetc

example.com/etcetc?query=aasd
example.com/etcetc?query=aasd&dest=asds

http://stackoverflow.com/questions/6427530/regular-expression-pattern-to-match-url-with-or-without-http-www
http://stackoverflow.com/questions/6427530/regular-expression-pattern-to-match-url-with-or-without-http-www/

Every valid internet URL has at least one dot, so the above pattern will simply try to find any at least two string chained by a dot, and has valid characters that URL may have.

每个有效的互联网 URL 至少有一个点,因此上述模式将简单地尝试找到任何至少两个由点链接的字符串,并且具有 URL 可能具有的有效字符。

回答by Jignesh Patel - Sr. HTML Dev.

Try this:

尝试这个:

/^http:\/\/|(www\.)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/

/^http:\/\/|(www\.)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/

It works exactly like the people want.

它的工作原理与人们想要的完全一样。

It takes with or with out http://, https://, and www.

它需要带或不带http://, https://, 和www

回答by Michael Wright

You can use a question mark after a regular expression to make it conditional so you would want to use:

您可以在正则表达式后使用问号使其成为有条件的,因此您可以使用:

http:\/\/(www\.)?

That will match anything that has either http://www. or http:// (with no www.)

这将匹配具有http://www 的任何内容。或 http://(没有 www。)

What you could do is just use a replace method to remove the above, thus getting you the domain. Depends on what you need the domain for.

您可以做的只是使用替换方法删除上述内容,从而获得域。取决于您需要域的用途。

回答by Nyveria

I know this is an old post, but just contributing my solution which is a combination of some of the answers I've found here on stackoverflow.

我知道这是一篇旧帖子,但只是贡献我的解决方案,这是我在 stackoverflow 上找到的一些答案的组合。

/(https?://)?((?:(\w+-)*\w+)\.)+(?:[a-z]{2})(\/?\w?-?=?_?\??&?)+[\.]?([a-z0-9\?=&_\-%#])?/g

Matches something.com, http(s)://or www. Does not match other [something]://urls though, but for my purpose that's not necessary.

匹配something.comhttp(s)://www[something]://虽然不匹配其他url,但对于我的目的来说,这是没有必要的。

The regex matches e.g.:

正则表达式匹配例如:

http://foo.co.uk/
www.regex.com/foo.html?q=bar$some=thi-ng,regex
regex.foo.com/blog

回答by morja

Try something like this:

尝试这样的事情:

.*([\w-]+\.)+[a-z]{2,5}(/[\w-]+)*

回答by K6t

Try this

尝试这个

$url_reg = /(ftp|https?):\/\/(\w+:?\w*@)?(\S+)(:[0-9]+)?(\/([\w#!:.?+=&%@!\/-])?)?/;

回答by Haris Thohir

you can try this:

你可以试试这个:

r"(http[s]:\/\/)?([\w-]+\.)+([a-z]{2,5})(\/+\w+)? "

selection :
1. may be start with http:// or https:// (optional)
2. anything (word) end with dot (.)
3. followed by 2 to 5 character [a-z]
4. followed by "/[anything]" (optional)
5. followed by space

选择:
1. 可以以 http:// 或 https:// 开头(可选)
2. 任何(单词)以点 (.) 结尾
3. 后跟 2 到 5 个字符 [az]
4. 后跟“/[任何东西]”(可选)
5. 后跟空格

回答by Mederic

I was getting so many issues get the answer from @anubhavadue to recent php allowing $in strings and the preg match wasn't working.

由于最近的 php 允许字符串并且 preg 匹配不起作用,我从@anubhava那里得到了很多问题的答案$

Here is what I used:

这是我使用的:

// regex
$re = '/((https?|ftp):\/\/)?([a-z0-9+!*(),;?&=.-]+(:[a-z0-9+!*(),;?&=.-]+)?@)?([a-z0-9\-\.]*)\.(([a-z]{2,4})|([0-9]{1,3}\.([0-9]{1,3})\.([0-9]{1,3})))(:[0-9]{2,5})?(\/([a-z0-9+%-]\.?)+)*\/?(\?[a-z+&$_.-][a-z0-9;:@&%=+\/.-]*)?(#[a-z_.-][a-z0-9+$%_.-]*)?/i';
// match all
preg_match_all($re, $blob, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
// the first element of the array is the full match

回答by Mark Tomlin

If it does not have to be regex, you could always use the Validate filters that are in PHP.

如果它不必是正则表达式,您始终可以使用 PHP 中的 Validate 过滤器。

filter_var('http://example.com', FILTER_VALIDATE_URL);

filter_var(mixed$variable [, int$filter = FILTER_DEFAULT[, mixed$options ]]);

filter_var(混合$variable [, int$filter = FILTER_DEFAULT[,混合$options ]]);

Types of Filters

过滤器类型

Validate Filters

验证过滤器