Regex Vimeo Parser

Today’s Regex is used to parse a string for Vimeo links and extract the Video_ID. Once we have the Video_ID we’re able to create a standard embed code from Vimeo links.

Supported Links:

The following Regex supports these Vimeo links and embed code snippets.

Normal URL: http://vimeo.com/9669721
Group URL:   http://vimeo.com/groups/tvc/videos/32626014
New Embed:   <iframe src="http://player.vimeo.com/video/9669721?title=0
             &byline=0&portrait=0&color=ffffff" width="400" 
             height="225" frameborder="0" webkitAllowFullScreen 
             mozallowfullscreen allowFullScreen > </iframe >

**** Doesn't handle Vimeo's old embed code ****

Again, I’ve broken it up the Regex and commented each line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$regexstr = '~
 # Match Vimeo link and embed code
(?:<iframe [^>]*src=")? 	# If iframe match up to first quote of src
(?:				# Group vimeo url
	https?:\/\/		# Either http or https
	(?:[\w]+\.)*		# Optional subdomains
	vimeo\.com		# Match vimeo.com
	(?:[\/\w]*\/videos?)?	# Optional video sub directory this handles groups links also
	\/			# Slash before Id
	([0-9]+)		# $1: VIDEO_ID is numeric
	[^\s]*			# Not a space
)				# End group
"?				# Match end quote if part of src
(?:[^>]*></iframe>)?		# Match the end of the iframe
(?:<p>.*</p>)?		        # Match any title information stuff
~ix';

Usage Example:

This example function takes an input string and uses the above Regex to parse off the Vimeo Video_ID ($1) and add it to $iframestr to create a standard embed code.

1
2
3
4
5
6
function ParsePostVimeo($string){
	$regexstr = <<REGEX FROM ABOVE>>;
	$iframestr = ' <p><iframe src="http://player.vimeo.com/video/$1?title=0&amp;byline=0&amp;portrait=0" width="500" height="284" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe></iframe></p> ';
 
	return preg_replace($regexstr, $iframestr, $string);
}

For more Regex see Regex YouTube Parser

I began programming in C++ when I was in college. Odd for a business major, but hey I am a Dork. After college I got a job as System Administrator. As a System Administrator I was in charge of web administration. My journey as a PHP web developer had begun. Since that time I have gained an in depth knowledge of CSS, Javascript, XML and MySQL. With changes and advances to technology I have also began learning AJAX. I started Blue Fire Development to do freelance work in my spare time.

Posted in Computers, PHP, Programming, Regex Tagged with: , ,
One comment on “Regex Vimeo Parser
  1. Viktor Ivanov says:

    Hi,
    we’ve revised and tweaked a bit your code to match more vimeo scheme links.

    http://regexr.com/393r3

1 Pings/Trackbacks for "Regex Vimeo Parser"

Leave a Reply

Your email address will not be published. Required fields are marked *

*