paulomartinhago
11/5/2014 - 6:50 PM

iframe de um vídeo do youtube e/ou vimeo.

iframe de um vídeo do youtube e/ou vimeo.

<?php

function iframeVideoUrl($url, $width = 640, $height = 360){
	if( strpos($url, 'vimeo') ){

		$url_parts = explode('/', $url);

		foreach ($url_parts as $value) {
			if( ! is_numeric($value) )
				continue;

			$id = $value;
		}

		$iframe = sprintf('<iframe width="%s" height="%s" src="http://player.vimeo.com/video/%s?title=0&amp;byline=0&amp;portrait=0&amp;badge=0&amp;color=ffffff" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowfullscreen></iframe>', $width, $height, $id);

	} else if( strpos($url, 'youtube') ){

		preg_match(
			'/[\\?\\&]v=([^\\?\\&]+)/',
			$url,
			$matches
		);

		$id     = $matches[1];
		$iframe = sprintf('<iframe width="%s" height="%s" src="//www.youtube.com/embed/%s" frameborder="0" allowfullscreen></iframe>', $width, $height, $id);

	} else {
		$iframe = 'O vídeo não pertence ao Youtube e/ou Vimeo.';
	}

	return $iframe;
}

// Exemplo de uso
echo iframeVideoUrl('https://www.youtube.com/watch?v=sg3LO56gG4U', 500, 400); // Os tamanhos são opcionais