Skip to content

Instantly share code, notes, and snippets.

@helloris25
Created September 16, 2015 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helloris25/9ee143273e9d8212d9ab to your computer and use it in GitHub Desktop.
Save helloris25/9ee143273e9d8212d9ab to your computer and use it in GitHub Desktop.
RegExp YouTube video iFrame | object
$regexstr = '~
# Match Youtube link and embed code
(?: # Group to match embed codes
(?:<iframe [^>]*src=")? # If iframe match up to first quote of src
|(?: # Group to match if older embed
(?:<object .*>)? # Match opening Object tag
(?:<param .*</param>)* # Match all param tags
(?:<embed [^>]*src=")? # Match embed tag to the first quote of src
)? # End older embed code group
)? # End embed code groups
(?: # Group youtube url
https?:\/\/ # Either http or https
(?:[\w]+\.)* # Optional subdomains
(?: # Group host alternatives.
youtu\.be/ # Either youtu.be,
| youtube\.com # or youtube.com
| youtube-nocookie\.com # or youtube-nocookie.com
) # End Host Group
(?:\S*[^\w\-\s])? # Extra stuff up to VIDEO_ID
([\w\-]{11}) # $1: VIDEO_ID is numeric
[^\s]* # Not a space
) # End group
"? # Match end quote if part of src
(?:[^>]*>)? # Match any extra stuff up to close brace
(?: # Group to match last embed code
</iframe> # Match the end of the iframe
|</embed></object> # or Match the end of the older embed
)? # End Group of last bit of embed code
~ix';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment