PHP: How to check whether the URL is Youtube's or vimeo's

后端 未结 8 2008
感情败类
感情败类 2021-02-02 01:19

How can I write a function to check whether the provided URLs is youtube or vimeo?

For instance, I have this two URLs that I store in a database as strings,



        
8条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-02 01:39

    You can try my solution:

    function checkServer( $domains=array(), $url ) {
        foreach ( $domains as $domain ) {
            if ( strpos($url, $domain ) > 0) {
                return true;
            } else {
                return false;
            }
        }
    }
    

    Use:

    if( checkServer(array("youtube.com","youtu.be"), $url ) ) {
        //is Youtube url
    }
    elseif( checkServer(array("vimeo.com"), $url ) ) {
        //is Vimeo
    }
    elseif ( checkServer(array("any domain"), $url ) ) {
        //is Any Domain
    }else {
        //unknow domain
    }
    

提交回复
热议问题