Search code examples
phpregexhtml-parsingpreg-matchtext-extraction

Get video id from the url path of an <iframe>'s src value


I need to get the vimeo ID of this iframe code, which is my string.

$str = '<iframe src="http://player.vimeo.com/video/34134823?title=0&amp;byline=0" width="900" height="506" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>'

I believe the ID part 34134823 can be more or less than 8 characters so I can't use indexes.

I could do it with 2 explodes, one for http://player.vimeo.com/video/ and than one for the ? question mark, but I am sure it would be better with a regex.


Solution

  • <?php
    $str = '<iframe src="http://player.vimeo.com/video/34134823?title=0&amp;byline=0" width="900" height="506" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
    
    $stat = preg_match('/.*video\/(\d{1,})\?title.*/', $str, $matches);
    
    print_r($matches);