Get specific snippet of a [duplicate] string

3

I have a url from youtube and would like to get only the excerpt from the variable "v".

I used this: $video = mb_substr('https://www.youtube.com/watch?v=k4qVkWh1EAo', 32) , it returns the value I need, but I would like to know if it has any other way.

You can directly access the variable "v" and get its value, is it possible in a string?

    
asked by anonymous 05.12.2016 / 13:16

1 answer

4

You can use parse_str and parse_url :

<?php
$url = "https://www.youtube.com/watch?v=k4qVkWh1EAo";
parse_str( parse_url( $url, PHP_URL_QUERY ), $vars);
echo $vars['v'];    
// Saída: k4qVkWh1EAo
?>
  

parse_str

     

Converts the string to variables

  

parse_url

     

Interprets a URL and returns its components

Note: It will not work if link is in the following format link

SOen Reference: PHP Regex to get youtube video ID?

    
05.12.2016 / 13:20