LoginSignup
5
4

More than 5 years have passed since last update.

【phpメモ】URLから動画のサービスを判定して然るべき埋め込みタグを出す

Last updated at Posted at 2015-09-11

以下の動画に対応
動画の幅などはお好みで

  • youtube
  • vimeo
  • vine
  • youku
  • tudou

/**
* 動画のURLからサービスを判定して埋め込みタグを生成する
*
* @param   string $param 動画URL
* @return  string        埋め込みタグ
*/
public function createvideotag($param)
{
    $param .= '/';
    // youtube
    if(preg_match('#https?://www.youtube.com/.*#i',$param,$matches)){
        $parse_url = parse_url($param);
        $v_param = '';
        // 動画IDを取得
        if (preg_match('#v=([-\w]{11})#i', $parse_url['query'], $v_matches)) {
            $video_id = $v_matches[1];
            return '<iframe width="600" height="338" src="https://www.youtube.com/embed/' . $v_matches[1] . '" frameborder="0" allowfullscreen></iframe>';
        }
    }
    // vimeo
    if(preg_match('#https?://vimeo.com.*#i',$param,$matches)){
        foreach ($matches as $key => $value) {
            if (preg_match('#/(\d+)/#i', $value, $val_match)) {
                return '<iframe src="https://player.vimeo.com/video/' . $val_match[1] . '?color=e0dccb" width="600" height="338" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>';
            }
        }
    }
    // vine
    if(preg_match('#https?://vine.co/v/(.*?)/#i',$param,$matches)){
        return '<iframe src="https://vine.co/v/' . $matches[1] . '/embed/simple" width="600" height="338" frameborder="0"></iframe><script src="https://platform.vine.co/static/scripts/embed.js"></script>';
    }
    // youku
    if(preg_match('#https?://v.youku.com/v_show/id_(.*?).html#i',$param,$matches)){
        return '<iframe class="wpjam_video" width=600 height=338 src="http://player.youku.com/embed/' . $matches[1] . '" frameborder=0 allowfullscreen></iframe>';
    }
    // tudou
    // tudouだけちょっと不十分なので修正が必要...
    if(preg_match('#https?://www.tudou.com/.*#i', $param, $matches)){
        if (preg_match('#([-\w]+).html#i', $param, $code)) {
            return '<iframe width=600 height=338 src="http://www.tudou.com/programs/view/html5embed.action?code='. $code[1] .'" frameborder=0 allowfullscreen></iframe>';
        }
    }
}
5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4