Skip to content

Instantly share code, notes, and snippets.

@bordoni
Created September 15, 2011 22:34
Show Gist options
  • Save bordoni/1220668 to your computer and use it in GitHub Desktop.
Save bordoni/1220668 to your computer and use it in GitHub Desktop.
YouTube Regex
<?php
$string = "<iframe width=\"1280\" height=\"720\" src=\"http://www.youtube.com/embed/UoVCqOlWMZg?rel=0&amp;hd=1\" frameborder=\"0\" allowfullscreen></iframe>";
preg_match_all( '~
# Match non-linked youtube URL in the wild. (Rev:20110825)
https?:// # Required scheme. Either http or https.
(?:www\.)? # Optional www subdomain.
(?: # Group host alternatives.
youtu\.be/ # Either youtu.be,
| youtube\.com # or youtube.com followed by
\S* # Allow anything up to VIDEO_ID,
[^\w\-\s] # but char before ID is non-ID char.
) # End host alternatives.
([\w\-]{11}) # $1: VIDEO_ID is exactly 11 chars.
(?=[^\w\-]|$) # Assert next char is non-ID or EOS.
(?! # Assert URL is not pre-linked.
[?=&+%\w]* # Allow URL (query) remainder.
(?: # Group pre-linked alternatives.
[\'"][^<>]*> # Either inside a start tag,
| </a> # or inside <a> element text contents.
) # End recognized pre-linked alts.
) # End negative lookahead assertion.
[?=&+%\w]* # Consume any URL (query) remainder.
~ix', $string, $output_var );
print_r( $output_var );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment