Skip to content

Instantly share code, notes, and snippets.

@rbk
Created May 29, 2013 02:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbk/5667589 to your computer and use it in GitHub Desktop.
Save rbk/5667589 to your computer and use it in GitHub Desktop.
PHP function that turns vimeo and youtube urls in to videos.
<?php
function filter_video_links( $content ){
// Regular Expressions
$youtube_link = '@^\s*https?://(?:www\.)?(?:youtube.com/watch\?|youtu.be/)([^\s"]+)\s*$@im';
$vimeo_link = '@^\s*https?://(?:www\.)?(?:vimeo.com/)@im';
if( preg_match( $vimeo_link, $content ) ) {
// vimeo
$content = preg_replace( $vimeo_link, 'http://player.vimeo.com/video/', $content );
return $content;
} else if ( preg_match( $youtube_link, $content ) ) {
// youtube
$content = str_replace( 'http://www.youtube.com/watch?v=', 'http://www.youtube.com/embed/', $content );
return $content;
} else {
return $content;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment