Comment

Shop Amazon's Black Friday Specials and Help Support LGF

141
Charles Johnson11/25/2016 3:24:10 pm PST

Welp, for the sheer heck of it, I just whipped up a regular expression that will handle it if you paste in a YouTube or Vimeo embed code, instead of the video page’s URL.

It looks a little something like this:

$text = preg_replace_callback(
	'~<iframe(?:.+?)src="https://(?:player|www)\.(?<site>vimeo|youtube)\.com/(?:video|embed)/(?<id>[^?"]*)\??[^"]*"[^>]*></iframe>(?:\s?<p><a href="https://vimeo.com/[^"]*"[^>]*>(?:.+?)</p>)?~i',
	function($matches) {
		if (strcasecmp($matches['site'], 'vimeo') === 0) {
			return 'https://vimeo.com/' . $matches['id'] . "\r\n";
		} else {
			return 'https://www.youtube.com/watch?v=' . $matches['id'] . "\r\n";
		}
	},
	$text
);

Now there will no longer be random looking bits of iframe code left over. Just a beautiful playable video.