如何将随机域名转换为小写一致的URL?

I have this function in a class:

protected $supportedWebsitesUrls = ['www.youtube.com', 'www.vimeo.com', 'www.dailymotion.com'];

protected function isValid($videoUrl)
{
    $urlDetails = parse_url($videoUrl);

    if (in_array($urlDetails['host'], $this->supportedWebsitesUrls))
    {
        return true;
    } else {
        throw new \Exception('This website is not supported yet!');

        return false;
    }

}

It basically extracts the host name from any random url and then checks if it is in the $supportedWebsitesUrls array to ensure that it is from a supported website. But if I add say: dailymotion.com instead of www.dailymotion.com it won't detect that url. Also if I try to do WWW.DAILYMOTION.COM it still won't work. What can be done? Please help me.

You can use preg_grep function for this. preg_grep supports regex matches against a given array.

Sample use:

$supportedWebsitesUrls = array('www.dailymotion.com', 'www.youtube.com', 'www.vimeo.com');

$s = 'DAILYMOTION.COM';

if ( empty(preg_grep('/' . preg_quote($s, '/') . '/i', $supportedWebsitesUrls)) )
   echo 'This website is not supported yet!
';
else
   echo "found a match
";

Output:

found a match

You can run a few checks on it;

For lower case vs upper case, the php function strtolower() will sort you out.

as for checking with the www. at the beginning vs without it, you can add an extra check to your if clause;

if (in_array($urlDetails['host'], $this->supportedWebsitesUrls) || in_array('www.'.$urlDetails['host'], $this->supportedWebsitesUrls))