Parse Youtube ID from Youtube embed code

Discussion in 'PHP' started by dshah, Jan 10, 2007.

  1. #1
    Could someone give me help with parsing out Youtube ID from youtube embed code

    Input:
    <object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/q_9cL9hfZs4"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/q_9cL9hfZs4" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>
    Code (markup):
    Output:
    q_9cL9hfZs4
    Code (markup):
     
    dshah, Jan 10, 2007 IP
    zytex likes this.
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    
    preg_match('/youtube\.com\/v\/([\w\-]+)/', $code, $match);
    
    echo $match[1];
    
    
    PHP:
    Untested.
     
    nico_swd, Jan 10, 2007 IP
    mihaidamianov likes this.
  3. roaryyyj

    roaryyyj Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    nico_swd, if the function is putting the result into an array (which i think it is), wouldnt the first bit be addressed by 0, not 1?
     
    roaryyyj, Jan 10, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    $match[0] exists, but I believe it holds the entire result, while $match[1] holds the code (video ID) we actually want.
     
    nico_swd, Jan 10, 2007 IP
  5. roaryyyj

    roaryyyj Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    ah yes i just tested it and you are right :)
     
    roaryyyj, Jan 10, 2007 IP