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

前端 未结 4 922
一向
一向 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);
    

提交回复
热议问题