dongyuzhu2244 2015-07-04 10:22
浏览 86
已采纳

如何将随机域名转换为小写一致的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.

  • 写回答

2条回答 默认 最新

  • dsorecdf78171 2015-07-04 10:32
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?