【问题标题】:How to get the id of youtube videos in laravel如何在 laravel 中获取 youtube 视频的 id
【发布时间】:2023-03-11 14:41:01
【问题描述】:

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

$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?

【问题讨论】:

  • 请 dd($videoId[1])
  • array:2 [▼ 0 => "youtube.com/watch?v=Y9oLMvwe4Eg" 1 => "Y9oLMvwe4Eg" ]
  • 这是 $videoId 还是 videoId[1]
  • 转储问题不在这里

标签: laravel youtube


【解决方案1】:

使用 parse_url() 和 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

【讨论】:

  • 使用你的代码我这样做了: parse_str( parse_url( $url, PHP_URL_QUERY ), $youtube ); $id = $youtube['v'];它工作正常,或者我只是将这一行添加到我的初始代码中:$id = $videoId[1];
猜你喜欢
  • 2023-03-17
  • 1970-01-01
  • 2016-09-28
  • 2012-05-22
  • 2011-03-28
  • 2012-08-14
  • 2014-12-15
  • 2019-12-07
相关资源
最近更新 更多