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:
    595
    Likes Received:
    30
    Best Answers:
    18
    Trophy Points:
    100
    #2
    I wouldn't use preg_match unless needed, this should be more handy:

    PHP:
    1.  
    2. $var  = parse_url('https://www.youtube.com/watch?v=oD26Mrf1mck&extra_param=test', PHP_URL_QUERY);
    3. parse_str($var, $var);
    4. echo $var['v'];
    5.  
    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 Well-Known Member

    Messages:
    1,224
    Likes Received:
    109
    Best Answers:
    51
    Trophy Points:
    160
    #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,119
    Likes Received:
    331
    Best Answers:
    17
    Trophy Points:
    325
    #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 Well-Known Member

    Messages:
    1,224
    Likes Received:
    109
    Best Answers:
    51
    Trophy Points:
    160
    #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,225
    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:
    1.  
    2. <?php
    3. $URI = 'http://www.youtube.com/watch?v=jNQXAC9IVRw&extra_param=foobar&more-foobar';
    4. preg_match('/=(.*?)&/', $URI, $var);
    5.  
    6. echo $var[1];
    7. ?>
    8.  
    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