Extract youtube video id with eval?

Discussion in 'Ruby' started by comopasta Gr, Apr 7, 2008.

  1. comopasta Gr

    comopasta Gr Guest

    comopasta Gr, Apr 7, 2008
    #1
    1. Advertising

  2. comopasta Gr

    comopasta Gr Guest

    Hmm. Just read some stuff about eval. The video url is initially
    provided by users. So is it wise to use eval for that (?)
    --
    Posted via http://www.ruby-forum.com/.
     
    comopasta Gr, Apr 7, 2008
    #2
    1. Advertising

  3. comopasta Gr

    Jano Svitok Guest

    On Mon, Apr 7, 2008 at 10:21 PM, comopasta Gr <> 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: http://www.ruby-forum.com/topic/143788
     
    Jano Svitok, Apr 7, 2008
    #3
  4. comopasta Gr

    comopasta Gr Guest

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

    Cheers
    --
    Posted via http://www.ruby-forum.com/.
     
    comopasta Gr, Apr 7, 2008
    #4
  5. On Monday 07 April 2008, comopasta Gr wrote:
    > Hi,
    >
    > I'm just doing the research but maybe someone already knows...
    >
    > I have a youtube video url:
    > http://www.youtube.com/watch?v=n1NVfDlU6yQ&feature=related
    > (&feature might or might not be present)
    >
    > I can get the thum of the video using:
    > 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


    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"

    I hope this helps

    Stefano
     
    Stefano Crocco, Apr 7, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. railsaware.com
    Replies:
    0
    Views:
    315
    railsaware.com
    Nov 6, 2006
  2. Jassim Rahma

    youtube like video?

    Jassim Rahma, Aug 2, 2007, in forum: ASP .Net
    Replies:
    12
    Views:
    810
    Laurent Bugnion, MVP
    Aug 4, 2007
  3. kkkk

    New youtube video

    kkkk, Apr 17, 2008, in forum: Python
    Replies:
    0
    Views:
    326
  4. toedipper
    Replies:
    2
    Views:
    437
    Travis Newbury
    Aug 12, 2008
  5. Eustace

    YouTube video code

    Eustace, Nov 26, 2009, in forum: HTML
    Replies:
    9
    Views:
    1,018
    dorayme
    Nov 27, 2009
Loading...

Share This Page