Get 11 characters to right of searched out string

Discussion in 'PHP' started by adbox, Dec 4, 2009.

  1. #1
    adbox, Dec 4, 2009 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $link = 'http://www.youtube.com/watch?v=16afe495';
    preg_match('%\?v=([^&]+)%', $link, $matches);
    echo $matches[1];
    PHP:
    Totally untested but that should do it
     
    JAY6390, Dec 4, 2009 IP
  3. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #3
    Oh, thank you, I dont have the link in a variable though, I have to detect it out of a string.
     
    adbox, Dec 4, 2009 IP
  4. xhanch

    xhanch Member

    Messages:
    683
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    35
    #4
    try this

    
    $page_source = '<retrived via CURL/fopen contain the HTML code of the website page>';
    $flag = 'http://www.youtube.com/watch?v=';
    $pos_start = strpos($page_source, $flag) + strlen($flag);
    //The next 11 characters
    $video_id = substr($page_source, $pos_start, 11);
    		
    
    PHP:
    Hope it works well! [​IMG]
     
    xhanch, Dec 4, 2009 IP
  5. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Well whatever the string is, put that instead of $link. It should still match it
     
    JAY6390, Dec 4, 2009 IP
  6. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #6
    It took some work, but xhanch's method works more constantly in the end.
     
    Last edited: Dec 4, 2009
    adbox, Dec 4, 2009 IP
  7. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Could also use
    $link = 'http://www.youtube.com/watch?v=16asdfsdfsdfsdffe495';
    preg_match('%watch\?v=(.{11})%', $link, $matches);
    echo $matches[1];
    PHP:
    I'm guessing that my code matched more than 11 characters?
     
    JAY6390, Dec 4, 2009 IP
  8. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #8
    Yeah, it pulled some extra to the right.
     
    adbox, Dec 4, 2009 IP
  9. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #9
    yeah after looking I thought it might. The above that I provided should only pull 11
     
    JAY6390, Dec 4, 2009 IP
  10. xhanch

    xhanch Member

    Messages:
    683
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    35
    #10
    Well, if you are just after a single link, you should use my method. If you after multiple video links, you should regex as given by jay.
     
    xhanch, Dec 4, 2009 IP
  11. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #11
    True, although you'd have to use preg_match_all instead
     
    JAY6390, Dec 4, 2009 IP