Regex for dropping http:// and www. from URLs

前端 未结 4 838
一向
一向 2020-12-04 00:34

I have a bunch of urls like these.

  $urls = array(
    \'https://site1.com\',
    \'https://www.site2.com\',
    \'http://www.site3.com\',
    \'https://sit         


        
相关标签:
4条回答
  • 2020-12-04 00:55

    preg_replace can also take an array, so you don't even need the loop. You can do this with a one liner:

    $urls = preg_replace('/(?:https?:\/\/)?(?:www\.)?(.*)\/?$/i', '$1', $urls);
    
    0 讨论(0)
  • 2020-12-04 00:55

    Short and sweet:

    $urls = preg_replace('~^(?:https?://)?(?:www[.])?~i', '', $urls);
    
    0 讨论(0)
  • 2020-12-04 00:56

    Depending on what exactly it is you want to do, it might be better to stick with PHP's own URL parsing facilities, namely parse_url:

    foreach ($urls as &$url) {
        $url = preg_replace('~^www.~', '', parse_url($url, PHP_URL_HOST));
    }
    unset($url);
    

    parse_url will give you the host of the URL, even if it will contain a port number or HTTP authentication data. (Whether this is what you need, depends on your exact use case though.)

    0 讨论(0)
  • 2020-12-04 01:03
    /^(https?:\/\/)?(www\.)?(.*)\/$/i
    

    And use what's on $3. Or, even better, change the first two parentheses to the non-capturing version (?:) and use what's on 1.

    0 讨论(0)
提交回复
热议问题