Skip to content

Instantly share code, notes, and snippets.

@kirkegaard kirkegaard/m2embed.php
Last active Jan 31, 2019

Embed
What would you like to do?
m2embed
<?php
$title = (isset($_GET['t'])) ? $_GET['t'] : false;
$content = (isset($_GET['s'])) ? $_GET['s'] : false;
$providers = [
'drop' => [
'regex' => '/https?:\/\/files\.m2film\.dk\/(.*)/',
'embed' => '<video controls src="https://files.m2film.dk/{code}&video=true"></video>',
],
'youtube' => [
'regex' => '%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i',
'embed' => '<iframe src="https://www.youtube.com/embed/{code}" allowfullscreen></iframe>',
],
'vimeo' => [
'regex' => '~(?:<iframe [^>]*src=")?(?:https?:\/\/(?:[\w]+\.)*vimeo\.com(?:[\/\w]*\/videos?)?\/([0-9]+)[^\s]*)"?(?:[^>]*></iframe>)?(?:<p>.*</p>)?~ix',
'embed' => '<iframe src="https://player.vimeo.com/video/{code}" allowfullscreen></iframe>',
],
];
preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $content, $matches);
$urls = $matches[0];
$embeds = [];
foreach ($urls as $url) {
foreach ($providers as $type => $provider) {
$res = preg_match($provider['regex'], $url, $match);
if ($res) {
array_push($embeds, str_replace('{code}', $match[1], $provider['embed']));
}
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<title>m2embed</title>
<style>
body {
background-color: #111;
color: #eee;
}
iframe {
border: 0;
}
</style>
</head>
<body>
<div class="container">
<?php if (empty($embeds)): ?>
<div class="row my-5">
<div class="col">
<form action="">
<div class="form-group">
<label for="t">Title</label>
<input type="text" name="t" id="t" placeholder="Title" class="form-control">
</div>
<div class="form-group">
<label for="s">Links</label>
<textarea name="s" id="s" rows="10" class="form-control"></textarea>
</div>
<input type="submit" class="btn btn-primary">
</form>
</div>
</div>
<?php else: ?>
<?php if ($title): ?>
<div class="row my-5">
<div class="col">
<h1><?php print $title?></h1>
</div>
</div>
<?php endif;?>
<?php foreach ($embeds as $embed): ?>
<div class="row my-5">
<div class="col">
<div class="embed-responsive embed-responsive-16by9">
<?php print $embed;?>
</div>
</div>
</div>
<?php endforeach;?>
<?php endif;?>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.