Regular expression for checking website url

后端 未结 6 602
轻奢々
轻奢々 2020-12-03 14:35

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

if user type url as

  1. www.test.com
  2. http://www.test.com
  3. https://www.test.com
相关标签:
6条回答
  • 2020-12-03 15:17

    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
    
    0 讨论(0)
  • 2020-12-03 15:18

    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!!!

    0 讨论(0)
  • 2020-12-03 15:27

    Use the following program for validating the website URL.

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

    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";
                }
        ?>
    
    
    
    0 讨论(0)
  • 2020-12-03 15:27

    You can make the http:// optional and also the s in https optional as:

    /^((?: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_]+)*)$/
    
    0 讨论(0)
  • 2020-12-03 15:29

    Pattern

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

    This will work for matching:

    www.demo.com

    http://foo.co.uk/

    0 讨论(0)
  • 2020-12-03 15:36

    Try the following instead :

    filter_var($your_variable, FILTER_VALIDATE_URL);
    
    0 讨论(0)
提交回复
热议问题