php 用于检查网站 url 的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2490310/
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-25 06:41:32  来源:igfitidea点击:

Regular expression for checking website url

phpregex

提问by Linto davis

I need to check the web address, using regular expression.

我需要使用正则表达式检查网址。

if user type url as

如果用户键入 url 作为

  1. www.test.com
  2. http://www.test.com
  3. https://www.test.com
  1. www.test.com
  2. http://www.test.com
  3. https://www.test.com

i have a regular expression like

我有一个正则表达式,比如

/^(http\:\/\/[a-zA-Z0-9_\-]+(?:\.[a-zA-Z0-9_\-]+)*\.[a-zA-Z]{2,4}(?:\/[a-zA-Z0-9_]+)*(?:\/[a-zA-Z0-9_]+\.[a-zA-Z]{2,4}(?:\?[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)?)?(?:\&[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)*)$/

but it will only allow the second option only. how can i modify the regular expression so that , it should accept 1st and 3rd option too

但它只允许第二个选项。我如何修改正则表达式,以便它也应该接受第一个和第三个选项

回答by Salman A

This is a pretty basic expression for testing domain names:

这是用于测试域名的非常基本的表达式:

@^(http\:\/\/|https\:\/\/)?([a-z0-9][a-z0-9\-]*\.)+[a-z0-9][a-z0-9\-]*$@i

Should match:

应该匹配:

domain.com
www.domain.com
http://domain.com
https://domain.com

回答by rekha_sri

Use the following program for validating the website URL.

使用以下程序验证网站 URL。

It will be validating the following and any valid website URL.

它将验证以下和任何有效的网站 URL。

  1. www.test.com
  2. http://www.test.com
  3. https://www.test.com
  1. www.test.com
  2. http://www.test.com
  3. https://www.test.com

    <?php
            $string_url="https://www.test.com";
            $reg_exp = "/^(http(s?):\/\/)?(www\.)+[a-zA-Z0-9\.\-\_]+(\.[a-zA-Z]{2,3})+(\/[a-zA-Z0-9\_\-\s\.\/\?\%\#\&\=]*)?$/";
            if(preg_match($reg_exp, $string_url) == TRUE){
            echo "URL is valid format";
            }
            else{
            echo "URL is invalid format";
            }
    ?>


回答by codaddict

You can make the http://optional and also the sin httpsoptional as:

您可以在http://可选的,也是shttps可选为:

/^((?:http(?:s)?\:\/\/)?[a-zA-Z0-9_-]+(?:.[a-zA-Z0-9_-]+)*.[a-zA-Z]{2,4}(?:\/[a-zA-Z0-9_]+)*(?:\/[a-zA-Z0-9_]+.[a-zA-Z]{2,4}(?:\?[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)?)?(?:\&[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)*)$/

回答by Mohd Haider

You can use this pattern also:

您也可以使用此模式:

(http(s)?://)?([\w-]+\.)+[\w-]+(/[\w- ;,./?%&=]*)?

This will work for matching:

这将适用于匹配:

http://www.google.com
https://www.google.com
www.google.com
google.com
google.in

Simple pattern for matching.

简单的匹配模式。

Hope this will help someone!!!

希望这会帮助某人!!!

回答by Swadesh Ranjan Dash

Pattern

图案

(https?:\/\/)?([\w\d]+\.)?[\w\d]+\.\w+\/?.+

(https?:\/\/)?([\w\d]+\.)?[\w\d]+\.\w+\/?.+

This will work for matching:

这将适用于匹配:

www.demo.com

http://foo.co.uk/

www.demo.com

http://foo.co.uk/

回答by 1ce

Try the following instead :

请尝试以下操作:

filter_var($your_variable, FILTER_VALIDATE_URL);