How to get the id of youtube videos in laravel

我是研究僧i 提交于 2019-12-24 01:47:10

问题


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
  }

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 ?


回答1:


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

Source: https://stackoverflow.com/a/3393008/320487

References:

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

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



来源:https://stackoverflow.com/questions/48559146/how-to-get-the-id-of-youtube-videos-in-laravel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!