Skip to content

Instantly share code, notes, and snippets.

@kirkegaard
Last active January 31, 2019 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirkegaard/c8a4249b951eb07fa894ef9fa2f69d7d to your computer and use it in GitHub Desktop.
Save kirkegaard/c8a4249b951eb07fa894ef9fa2f69d7d to your computer and use it in GitHub Desktop.
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