Created
September 15, 2011 22:34
-
-
Save bordoni/1220668 to your computer and use it in GitHub Desktop.
YouTube Regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$string = "<iframe width=\"1280\" height=\"720\" src=\"http://www.youtube.com/embed/UoVCqOlWMZg?rel=0&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