I have a bunch of urls like these.
$urls = array( \'https://site1.com\', \'https://www.site2.com\', \'http://www.site3.com\', \'https://sit
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);