# Extract youtube video id with eval?

**URL:** <https://www.ruby-forum.com/t/extract-youtube-video-id-with-eval/134107>\
**Category:** Ruby\
**Created:** [April 7, 2008, 10:18pm UTC](https://www.ruby-forum.com/t/extract-youtube-video-id-with-eval/134107 "2008-04-07T22:18:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [April 7, 2008, 10:18pm UTC](https://www.ruby-forum.com/t/extract-youtube-video-id-with-eval/134107/1 "2008-04-07T22:18:13Z")

</div>

Hi,

I’m just doing the research but maybe someone already knows…

I have a youtube video url:

[![]( " - YouTube") ](https://www.youtube.com/watch?v=n1NVfDlU6yQ)

(&feature might or might not be present)

I can get the thum of the video using:  
[http://img.youtube.com/vi/n1NVfDlU6yQ/default.jpg](http://img.youtube.com/vi/n1NVfDlU6yQ/default.jpg)

So, I just need to use the video id.

Now, how to extract the video id easily? Eval?  
Yeah simple I know…I’m working on it in the meanwhile.

Thanks

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [April 7, 2008, 10:21pm UTC](https://www.ruby-forum.com/t/extract-youtube-video-id-with-eval/134107/2 "2008-04-07T22:21:47Z")

</div>

Hmm. Just read some stuff about eval. The video url is initially  
provided by users. So is it wise to use eval for that (?)

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [April 7, 2008, 10:30pm UTC](https://www.ruby-forum.com/t/extract-youtube-video-id-with-eval/134107/3 "2008-04-07T22:30:29Z")

</div>

Looks like what I’m looking for is an equivalent to javascripts  
toQueryParams().  
Is there one?

Cheers

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [April 7, 2008, 10:31pm UTC](https://www.ruby-forum.com/t/extract-youtube-video-id-with-eval/134107/4 "2008-04-07T22:31:15Z")

</div>

On Mon, Apr 7, 2008 at 10:21 PM, comopasta Gr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hmm. Just read some stuff about eval. The video url is initially  
> provided by users. So is it wise to use eval for that (?)

Try using Regexps. BTW, somebody asked very similar question not too  
long ago: [Get value from string - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/143788)

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [April 7, 2008, 10:45pm UTC](https://www.ruby-forum.com/t/extract-youtube-video-id-with-eval/134107/5 "2008-04-07T22:45:24Z")

</div>

Thank you so much guys!

…and sorry for the topic duplication indeed it was covered before

Cheers!

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [April 7, 2008, 10:34pm UTC](https://www.ruby-forum.com/t/extract-youtube-video-id-with-eval/134107/6 "2008-04-07T22:34:16Z")

</div>

On Monday 07 April 2008, comopasta Gr wrote:

> So, I just need to use the video id.
> 
> Now, how to extract the video id easily? Eval?  
> Yeah simple I know…I’m working on it in the meanwhile.
> 
> Thanks

If you only need to extract the part of the url between watch?v= and  
either  
the end of the string or the &feature part, you can use a simple regexp:

id = url.match(/watch?v=(.\*?)(?:&feature|\Z)/)[1]

This matches the string 'watch?v= followed by any number of characters  
and by  
either the string &feature or the end of the string. The middle part  
(which I  
described as “any number of character”) are then stored in the first  
subgroup  
(the element with index 1 of the MatchData object returned by match).

You can create the image url with:

img\_url = “[http://img.youtube.com/vi/#{id}/default.jpg](http://img.youtube.com/vi/#%7Bid%7D/default.jpg)”

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [April 7, 2008, 10:46pm UTC](https://www.ruby-forum.com/t/extract-youtube-video-id-with-eval/134107/7 "2008-04-07T22:46:09Z")

</div>

On Tue, Apr 08, 2008 at 05:18:15AM +0900, comopasta Gr wrote:

> Now, how to extract the video id easily? Eval?  
> Yeah simple I know…I’m working on it in the meanwhile.

irb(main):001:0\> require ‘uri’  
=\> true  
irb(main):002:0\> uri =  
URI.parse(‘[- YouTube](http://www.youtube.com/watch?v=n1NVfDlU6yQ&feature=related)’)  
=\> #\<URI::HTTP:0x1b1066  
URL:[- YouTube](http://www.youtube.com/watch?v=n1NVfDlU6yQ&feature=related)\>  
irb(main):003:0\> require ‘cgi’  
=\> true  
irb(main):004:0\> query\_string = CGI.parse(uri.query)  
=\> {“feature”=\>[“related”], “v”=\>[“n1NVfDlU6yQ”]}

marcel
