[Function] Universal Embed

Written something you are proud of, post it here.
Post Reply
RistetToast
Posts: 1
Joined: Wed Mar 18, 2015 7:50 am

[Function] Universal Embed

Post by RistetToast »

Hello everyone :D

Today I would like to share my "Universal Embed" function ^_^ It is technically a function, which I have been working on from time to time during the past few years, whenever I have had the time to work on it, but today I sat down for some hours, and ended up giving it quite the upgrade :D

Before it used to only append the iframe of one, of the possibly countless links, to the bottom of the string. Today I wrote the entire "replace" part of the function, so that it would be more useful on e.g. a blog. :mrgreen: It simply finds all url's that matches YouTube videos, Vimeo videos, Spotify music, Soundcloud music, Twitch chat's, Twitch livestream's, and Facebook videos, and replaces those with their respective iframe or object ^__^ :D

As it supports most of the popular media embed objects at the moment, I might not add anything to the function for quite a while :) It have been my baby ever since I started to get the hang of PHP, and I don't feel like I can do much more with this function, at least until some future popular media creates an embed, so I thought I would just release it as it is ^_^ Also because I don't really spend all that much time with web development anymore :)

Please note that I am in no way a master of regular expressions, so some of them could possibly be cleaned up, but the important thing is that they all work :)

Feel free to use this for any project you would like, or even use it as a base and add your own embed codes to it :D I would appreciate if you would remember to give credit where credit is due :) I don't mean that you must put "This function is made by RistetToast" all over your code or something like that, but simply that you don't pull the "Yup, that lovely function is all made by me" card, when/if someone ask you about the function ^_^

I hope some of you guys will find this function as useful as I personally do :D Would love to see new takes on it (Additions, cleaner versions, etc), so if you do make one based on this, please leave it in a post below this so I can have a look; would be awesome ^_^

Note:
The code syntax highlight on the forum makes it look like a total mess. If you would like to have a better overview, I have provided the function in its own seperate file on my DropBox here:
https://db.tt/0WcNTdAO
// Universal embed function. Supports: YouTube, Vimeo, Facebook, Twitch, Soundcloud, and Spotify.
function embed( $link, $media = 0, $width = 300, $height = 300, $replace = 0 )
{
	if ( $replace )
	{
		if ( preg_match_all( '(youtube.com/watch\?v=[a-zA-Z0-9]|youtu.be/[a-zA-Z0-9])', $link ) )
		{
			$regex = preg_match_all( '~(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\s]+(?=\?)|(?<=v=)[^&\s]+|(?<=youtu.be/)[^&\s]~ix', $link, $youtube );

			for ( $i = 0; $i < sizeof( $youtube[0] ); $i++ )
			{		
				$search = array( 
					'http://youtube.com/watch?v='. $youtube[0][$i],
					'http://www.youtube.com/watch?v='. $youtube[0][$i],
					'https://youtube.com/watch?v='. $youtube[0][$i],
					'https://www.youtube.com/watch?v='. $youtube[0][$i]
				);
				
				$link = str_ireplace( $search, "<br><iframe width=\"{$width}\" height=\"{$height}\" src=\"https://www.youtube.com/embed/{$youtube[0][$i]}\" frameborder=\"0\" allowfullscreen></iframe><br>", $link );
			}
		}
		if ( preg_match_all( '(vimeo.com/[0-9])', $link ) )
		{
			$regex  = preg_match_all( '~(?:https?:\/\/(?:[\w]+\.)*vimeo\.com(?:[\/\w]*\/videos?)?\/([0-9]+)[^\s]*)"?~ix', $link, $vimeo );
			
			for ( $i = 0; $i < sizeof( $vimeo[0] ); $i++ )
			{
				$search = array( 
					'http://www.vimeo.com/'. $vimeo[1][$i],
					'https://www.vimeo.com/'. $vimeo[1][$i],
					'http://vimeo.com/'. $vimeo[1][$i],
					'https://vimeo.com/'. $vimeo[1][$i]
				);
				
				$link = str_ireplace( $search, "<br><iframe src=\"http://player.vimeo.com/video/{$vimeo[1][$i]}?title=0&byline=0&portrait=0\" width=\"{$width}\" height=\"{$height}\" frameborder=\"0\" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe><br>", $link );
			}
		}
		if ( preg_match_all( '((facebook.com)\/(photo|video)\.php\?v=[0-9])', $link ) )
		{
			$regex = preg_match_all( '~(http\:\/\/)?(facebook\.com)\/(photo|video)\.php\?v=([0-9]+)~ix', $link, $facebook );
			
			for ( $i = 0; $i < sizeof( $facebook[0] ); $i++ )
			{
				$search = array( 
					'http://facebook.com/photo.php?v='. $facebook[4][$i],
					'https://facebook.com/photo.php?v='. $facebook[4][$i],
					'http://www.facebook.com/video.php?v='. $facebook[4][$i],
					'https://www.facebook.com/video.php?v='. $facebook[4][$i]
				);

				$link = str_ireplace( $search, "<br><iframe src=\"https://www.facebook.com/video/embed?vi ... facebook[4][$i]}\" width=\"{$width}\" height=\"{$height}\" frameborder=\"0\"></iframe><br>", $link );
			}
		}
		if ( preg_match_all( '(soundcloud\.com\/[a-zA-Z0-9]+\/[a-zA-Z0-9]+)', $link ) )
		{
			$regex = preg_match_all( '~(http\:\/\/)?(soundcloud\.com)\/([a-zA-Z0-9]+)\/([a-zA-Z0-9]+)~ix', $link, $soundcloud );
			
			for ( $i = 0; $i < sizeof( $soundcloud[0] ); $i++ )
			{
				$search = array( 
					"http://soundcloud.com/{$soundcloud[3][$i]}/{$soundcloud[4][$i]}",
					"https://soundcloud.com/{$soundcloud[3][$i]}/{$soundcloud[4][$i]}",
					"http://www.soundcloud.com/{$soundcloud[3][$i]}/{$soundcloud[4][$i]}",
					"https://www.soundcloud.com/{$soundcloud[3][$i]}/{$soundcloud[4][$i]}"
				);

				$link = str_ireplace( $search, "<br><iframe width=\"{$width}\" height=\"{$height}\" scrolling=\"no\" frameborder=\"no\" src=\"https://w.soundcloud.com/player/?url=ht ... undcloud[3][$i]}/{$soundcloud[4][$i]}&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&visual=true\"></iframe><br>", $link );
			}
		}
		if ( preg_match_all( '(open.spotify.com\/(track|album|artist)\/([a-zA-Z0-9]))', $link ) )
		{
			$regex = preg_match_all( '~(http\:\/\/)?open.spotify.com\/(track|album|artist)\/([a-zA-Z0-9]{22})~ix', $link, $spotify );

			for ( $i = 0; $i < sizeof( $spotify[0] ); $i++ )
			{
				$search = array( 
					"http://open.spotify.com/{$spotify[2][$i]}/{$spotify[3][$i]}",
					"https://open.spotify.com/{$spotify[2][$i]}/{$spotify[3][$i]}",
					"http://www.open.spotify.com/{$spotify[2][$i]}/{$spotify[3][$i]}",
					"https://www.open.spotify.com/{$spotify[2][$i]}/{$spotify[3][$i]}"
				);
				
				$link = str_ireplace( $search, "<br><iframe src=\"https://embed.spotify.com/?uri=spotify:{$spotify[2][$i]}:{$spotify[3][$i]}\" width=\"{$width}\" height=\"{$height}\" frameborder=\"0\" allowtransparency=\"true\"></iframe><br>", $link );
			}
		}
		if ( preg_match_all( '(twitch\.tv\/([a-zA-Z0-9]+)\/chat)', $link ) )
		{
			$regex = preg_match_all( '~((http\:\/\/)?twitch\.tv\/([a-zA-Z0-9]+)\/chat)~ix', $link, $twitch );

			for ( $i = 0; $i < sizeof( $twitch[0] ); $i++ )
			{
				$search = array(
					"http://twitch.tv/{$twitch[3][$i]}/chat",
					"https://twitch.tv/{$twitch[3][$i]}/chat",
					"http://www.twitch.tv/{$twitch[3][$i]}/chat",
					"https://www.twitch.tv/{$twitch[3][$i]}/chat",
					"http://twitch.tv/{$twitch[3][$i]}/chat?popout=",
					"https://twitch.tv/{$twitch[3][$i]}/chat?popout=",
					"http://www.twitch.tv/{$twitch[3][$i]}/chat?popout=",
					"https://www.twitch.tv/{$twitch[3][$i]}/chat?popout="
				);

				$link = str_ireplace( $search, "<br><iframe frameborder=\"0\" scrolling=\"no\" src=\"http://twitch.tv/{$twitch[3][$i]}/chat\" height=\"{$height}\" width=\"{$width}\"></iframe>", $link );
			}
		}
		if ( preg_match_all( '(twitch.tv/[a-zA-Z0-9]+)', $link ) )
		{
			preg_match_all( '~(https?\:\/\/)?(www\.)?twitch\.tv\/([a-zA-Z0-9]+)(?!\/chat)\b~ix', $link, $twitch );
 
			for ( $i = 0; $i < sizeof( $twitch[0] ); $i++ )
			{
				$newstr = "<br><object type=\"application/x-shockwave-flash\" height=\"{$height}\" width=\"{$width}\" id=\"live_embed_player_flash\" data=\"http://www.twitch.tv/widgets/live_embed ... {$twitch[3][$i]}\" bgcolor=\"#000000\">
						<param name=\"allowFullScreen\" value=\"true\">
						<param name=\"allowScriptAccess\" value=\"always\">
						<param name=\"allowNetworking\" value=\"all\">
						<param name=\"movie\" value=\"http://www.twitch.tv/widgets/live_embed_player.swf\">
						<param name=\"flashvars\" value=\"hostname=www.twitch.tv&channel={$twitch[3][$i]}&auto_play=true&start_volume=25\">
				</object><br>";
			   
				$link   = preg_replace( "~(https?\:\/\/)?(www\.)?twitch\.tv\/({$twitch[3][$i]})(?!\/chat)\b~ix", $newstr, $link );
			}

		}
		
		return $link;
	}
	else
	{
		
		if ( preg_match( '(vimeo.com/[0-9])', $link ) )
		{
			$type = 'vimeo';
		}
		else if ( preg_match( '(youtube.com/watch\?v=[a-zA-Z0-9]|youtu.be/[a-zA-Z0-9])', $link ) )
		{
			$type = 'youtube';
		}
		else if ( preg_match( '(soundcloud\.com\/[a-zA-Z0-9]+\/[a-zA-Z0-9]+)', $link ) )
		{
			$type = 'soundcloud';
		}
		else if ( preg_match( '(open.spotify.com\/(track|album|artist)\/([a-zA-Z0-9]))', $link ) )
		{
			$type = 'spotify';
		}
		else if ( preg_match( '(facebook.com/photo.php\?v=[0-9])', $link ) )
		{
			$type = 'facebook';
		}
		else if ( preg_match( '(twitch.tv/[a-zA-Z0-9]+/chat)', $link ) )
		{
			$type = 'twitchchat';
		}
		else if ( preg_match( '(twitch.tv/[a-zA-Z0-9]+)', $link ) )
		{
			$type = 'twitch';
		}
		
		switch( $type )
		{
			case 'vimeo':
				$regex  = preg_match( '~(?:https?:\/\/(?:[\w]+\.)*vimeo\.com(?:[\/\w]*\/videos?)?\/([0-9]+)[^\s]*)"?~ix', $link, $vimeo );
				
				$regex2 = preg_match( '#[0-9]+#', $vimeo[0], $vimID );
				
				if ( $media )
				{
					$link = "<iframe src=\"http://player.vimeo.com/video/{$vimID[0 ... portrait=0\" width=\"{$width}\" height=\"{$height}\" frameborder=\"0\" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>";
				}
				else
				{
					$link .= "<br><iframe src=\"http://player.vimeo.com/video/{$vimID[0 ... portrait=0\" width=\"{$width}\" height=\"{$height}\" frameborder=\"0\" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe><br>";
				}
				
				return $link;
			break;
			
			case 'youtube':
				$regex     = preg_match( '~(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\s]+(?=\?)|(?<=v=)[^&\s]+|(?<=youtu.be/)[^&\s]~ix', $link, $youtube );
	 
				$youtubeId = $youtube[0];
				
				if ( $media )
				{
					$link = "<iframe width=\"{$width}\" height=\"{$height}\" src=\"https://www.youtube.com/embed/{$youtubeId}\" frameborder=\"0\" allowfullscreen></iframe>";
				}
				else
				{
					$link .= "<br><iframe width=\"{$width}\" height=\"{$height}\" src=\"https://www.youtube.com/embed/{$youtubeId}\" frameborder=\"0\" allowfullscreen></iframe><br>";
				}
						
				return $link;
			break;
			
			case 'soundcloud':
				$regex = preg_match( '~(http\:\/\/)?(soundcloud\.com)\/([a-zA-Z0-9]+)\/([a-zA-Z0-9]+)~ix', $link, $soundcloud );

				if ( $media )
				{
					$link = "<br><iframe width=\"{$width}\" height=\"{$height}\" scrolling=\"no\" frameborder=\"no\" src=\"https://w.soundcloud.com/player/?url=ht ... iframe><br>";
				}
				else
				{
					$link .= "<br><iframe width=\"{$width}\" height=\"{$height}\" scrolling=\"no\" frameborder=\"no\" src=\"https://w.soundcloud.com/player/?url=ht ... iframe><br>";
				}

				return $link;
			break;
			
			case 'spotify':
				$regex = preg_match( '~(http\:\/\/)?open.spotify.com\/(track|album|artist)\/([a-zA-Z0-9]{22})~ix', $link, $spotify );
				
				$uri   = "{$spotify[2]}:{$spotify[3]}";
				
				if ( $media )
				{
					$link = "<iframe src=\"https://embed.spotify.com/?uri=spotify:{$uri}\" width=\"{$width}\" height=\"{$height}\" frameborder=\"0\" allowtransparency=\"true\"></iframe>";
				}
				else
				{
					$link .= "<br><iframe src=\"https://embed.spotify.com/?uri=spotify:{$uri}\" width=\"{$width}\" height=\"{$height}\" frameborder=\"0\" allowtransparency=\"true\"></iframe><br>";
				}
						
				return $link;
			break;
			
			case 'facebook':
				$regex = preg_match( '~(http\:\/\/)?facebook.com\/photo.php\?v=([0-9]+)~ix', $link, $facebook );
				
				$fbid  = $facebook[2];
				
				if ( $media )
				{
					$link = "<iframe src=\"https://www.facebook.com/video/embed?video_id={$fbid}\" width=\"{$width}\" height=\"{$height}\" frameborder=\"0\"></iframe>";
				}
				else
				{
					$link .= "<br><iframe src=\"https://www.facebook.com/video/embed?video_id={$fbid}\" width=\"{$width}\" height=\"{$height}\" frameborder=\"0\"></iframe><br>";
				}
				
				return $link;
			break;
			
			case 'twitchchat':
				$regex = preg_match( '~((http\:\/\/)?twitch.tv/([a-zA-Z0-9]+)/chat)~ix', $link, $twitch );
				
				if ( $media )
				{
					$link = "<iframe frameborder=\"0\" scrolling=\"no\" src=\"http://twitch.tv/{$twitch[3]}/chat?popout=\" height=\"{$height}\" width=\"{$width}\"></iframe>";
				}
				else
				{
					$link .= "<br><iframe frameborder=\"0\" scrolling=\"no\" src=\"http://twitch.tv/{$twitch[3]}/chat?popout=\" height=\"{$height}\" width=\"{$width}\"></iframe><br>";
				}
				
				return $link;
			break;
			
			case 'twitch':
				$regex = preg_match( '~((http\:\/\/)?twitch.tv/([a-zA-Z0-9]+))~ix', $link, $twitch );
				
				if ( $media )
				{
					$link = "<object type=\"application/x-shockwave-flash\" height=\"{$height}\" width=\"{$width}\" id=\"live_embed_player_flash\" data=\"http://www.twitch.tv/widgets/live_embed ... {$twitch[3]}\" bgcolor=\"#000000\">
						<param name=\"allowFullScreen\" value=\"true\">
						<param name=\"allowScriptAccess\" value=\"always\">
						<param name=\"allowNetworking\" value=\"all\">
						<param name=\"movie\" value=\"http://www.twitch.tv/widgets/live_embed_player.swf\">
						<param name=\"flashvars\" value=\"hostname=www.twitch.tv&channel={$twitch[3]}&auto_play=true&start_volume=25\">
					</object>";
				}
				else
				{
					$link .= "<br><object type=\"application/x-shockwave-flash\" height=\"{$height}\" width=\"{$width}\" id=\"live_embed_player_flash\" data=\"http://www.twitch.tv/widgets/live_embed ... {$twitch[3]}\" bgcolor=\"#000000\">
						<param name=\"allowFullScreen\" value=\"true\">
						<param name=\"allowScriptAccess\" value=\"always\">
						<param name=\"allowNetworking\" value=\"all\">
						<param name=\"movie\" value=\"http://www.twitch.tv/widgets/live_embed_player.swf\">
						<param name=\"flashvars\" value=\"hostname=www.twitch.tv&channel={$twitch[3]}&auto_play=true&start_volume=25\">
					</object><br>";
				}
				
				return $link;
			break;
			
			default:
				return $link;
			break;
		}
	}
}
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: [Function] Universal Embed

Post by jacek »

Looks cool, thanks for sharing :)
Image
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: [Function] Universal Embed

Post by Temor »

This actually seems useful, I'll definitely check it out :)
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: [Function] Universal Embed

Post by Helx »

Ohmigod, this is actually the holy grail of embed functions!
Thankyou so so much! I will certainly remember it for a lil' project ;)
Post Reply