如何在Laravel中获取youtube视频的ID [英] How to get the id of youtube videos in laravel

查看:85
本文介绍了如何在Laravel中获取youtube视频的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取我使用此代码的youtube视频的ID:

I want to get the id of the youtube video i used this code :

$url = $video->link;
  if (preg_match('/youtube\.com\/watch\?v=([^\&\?\/]+)/', $url, $videoId)) {
    $values = $videoId[1];
  } else if (preg_match('/youtube\.com\/embed\/([^\&\?\/]+)/', $url, $videoId)) {
    $values = $videoId[1];
  } else if (preg_match('/youtube\.com\/v\/([^\&\?\/]+)/', $url, $videoId)) {
    $values = $videoId[1];
  } else if (preg_match('/youtu\.be\/([^\&\?\/]+)/', $url, $videoId)) {
    $values = $videoId[1];
  }
  else if (preg_match('/youtube\.com\/verify_age\?next_url=\/watch%3Fv%3D([^\&\?\/]+)/', $url, $id)) {
      $values = $videoId[1];
  } else {   
  // not an youtube video
  }

我收到此错误"htmlspecialchars()期望参数1为字符串,给定数组".错误是当我想要一个字符串时,此代码给了我一个数组,是否有解决方案以字符串格式获取youtube视频的ID?

I get this error "htmlspecialchars() expects parameter 1 to be string, array given" . the error is that this code give me an array when i want a string is there a solution to get the id of the youtube video in a string format ?

推荐答案

使用parse_url()和parse_str()

Use parse_url() and parse_str()

$url="http://www.youtube.com/watch?v=C4kxS1ksqtw"

parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array_of_vars );

echo $my_array_of_vars['v'];    

 // Output: C4kxS1ksqtw

来源: https://stackoverflow.com/a/3393008/320487

参考文献:

http://php.net/manual/en/function.parse -url.php

http://php.net/manual/en/function.parse -str.php

这篇关于如何在Laravel中获取youtube视频的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆