1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need some help with preg_match

Discussion in 'PHP' started by angryencoder, Oct 22, 2013.

  1. #1
    Hello Anders Falk here! I'm not so good with preg_match so this is why I have choose to create a thread here... Anyway I wonder how to just take out the video id from a youtube link? Only the video id.

    I got the site filmbladet.se and I really need help with this. So what can it be?... Some links looks like this and it's very annoying!

    http://www.youtube.com/watch?v=mgNmB...tra_param=true

    Other links have some sort of gdata_youtube behind.
     
    angryencoder, Oct 22, 2013 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    I wouldn't use preg_match unless needed, this should be more handy:

    
    $var  = parse_url('https://www.youtube.com/watch?v=oD26Mrf1mck&extra_param=test', PHP_URL_QUERY);
    parse_str($var, $var);
    echo $var['v'];
    
    PHP:
     
    ThePHPMaster, Oct 22, 2013 IP
    ROOFIS likes this.
  3. angryencoder

    angryencoder Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    I'm sorry but it needs to be preg_match! :D
     
    angryencoder, Oct 22, 2013 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Okay... I've just got to ask - why does it HAVE to be preg_match? Why use a function with way more overhead for something so simple as this? parse_url is there for a reason, so... why preg_match?
     
    PoPSiCLe, Oct 22, 2013 IP
  5. angryencoder

    angryencoder Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    Because I got an rss script that I post news from in filmbladet.se and those news youtube links be like youtube.com/watch?v=0iyeUcFKRv4=_g_data and more text... And the code can't embed if the link are like that. That's why we need to cut out the video id and cut in into the embed code!... And youtube.com/watch?v= will be there already!
     
    angryencoder, Oct 23, 2013 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Not sure if you've actually tried the code above, but it works just fine. It will just take the "v" variable from the URL and ignore the rest.
     
    nico_swd, Oct 23, 2013 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    The v=CodeGoesHere need to separate the rest of the variables in the URL somehow, normally with an ampersand as separator - the parse_url-function uses this to only get the parameter you want, and discard the rest. Hence, no need for preg_match...
     
    PoPSiCLe, Oct 23, 2013 IP
  8. ROOFIS

    ROOFIS Well-Known Member

    Messages:
    1,234
    Likes Received:
    30
    Best Answers:
    5
    Trophy Points:
    120
    #8
    I wholefully recommend utilizing parse_url / parse_str as ThePHPMaster has exampled and only switch to the regular expression engine when absolutely needed, mainly due to overhead issues.

    That being said I've coded below the preg_match equivalent to parse_url / parse_str coded from ThePHPMaster and offer it here for educational purposes only since your wanting to learn more about preg_match and regex


    
    <?php
    $URI = 'http://www.youtube.com/watch?v=jNQXAC9IVRw&extra_param=foobar&more-foobar';
    preg_match('/=(.*?)&/', $URI, $var);
    
    echo $var[1];
    ?>
    
    PHP:
    noting the greedy-less lazy match (.*?) syntax to save literal traversing time and energy from the regex parser and using it between first equals sign and ampersand in the URI.




    :D :D :D


    .
     
    ROOFIS, Oct 25, 2013 IP