0

I need to get the ID of a youtube video to dynamically embed the video in my webpage.

I am looking for a way to achieve the following in twig:

$url="https://www.youtube.com/watch?v=hqDT5_4z_YI"

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

echo $my_array_of_vars['v'];    

 // Output: hqDT5_4z_YI

Is this even possible or should I use JavaScript to do this?

mike
  • 445
  • 2
  • 4
  • 18

1 Answers1

2

You can use Javascript:

let testparams = new URL("https://www.youtube.com/watch?v=hqDT5_4z_YI").searchParams;
alert(testparams.get('v'));
protoproto
  • 2,081
  • 1
  • 13
  • 13
  • 1
    That works! But I ended up going for this solution : `{{% set url = block.blogContentVideo|split('v=', 2) %}` Could you tell me if this is a good way to do it? – mike Sep 10 '18 at 13:04
  • Oh, I don't know about your language! – protoproto Sep 10 '18 at 13:17