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条)

报告相同问题?

悬赏问题

  • ¥20 ue5运行的通道视频都会有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数