From 676ebc3746f03134ddc3e03351a6f11b4bd39fa6 Mon Sep 17 00:00:00 2001 From: Danang Probo Sayekti Date: Tue, 21 Jul 2015 00:37:57 +0700 Subject: [PATCH] Fix Youtube video ID --- system/htmly.php | 4 ++-- system/includes/functions.php | 14 +++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/system/htmly.php b/system/htmly.php index 4791886..c50d71b 100644 --- a/system/htmly.php +++ b/system/htmly.php @@ -1853,8 +1853,8 @@ get('/:year/:month/:name/edit', function ($year, $month, $name) { } elseif (isset($current->audio)) { $var = 'edit-audio'; } elseif (isset($current->video)) { - $var = 'edit-video'; } - else { + $var = 'edit-video'; + } else { $var = 'edit-post'; } diff --git a/system/includes/functions.php b/system/includes/functions.php index db9e88f..a01ad2e 100644 --- a/system/includes/functions.php +++ b/system/includes/functions.php @@ -267,7 +267,7 @@ function get_posts($posts, $page = 1, $perpage = 0) // Extract the title and body $post->title = get_content_tag('t', $content, 'Untitled: ' . date('l jS \of F Y', $post->date)); $post->image = get_content_tag('image', $content); - $post->video = str_replace(array("http://", "https://", "www.", "youtube", ".com", "/watch?v=", "/embed/"), "", get_content_tag('video', $content)); + $post->video = get_youtube_id(get_content_tag('video', $content)); $post->link = get_content_tag('link', $content); $post->quote = get_content_tag('quote', $content); $post->audio = get_content_tag('audio', $content); @@ -2011,3 +2011,15 @@ function isCaptcha($reCaptchaResponse) } return ($json['success']); } + +// Get YouTube video ID +function get_youtube_id($url) +{ + if(empty($url)) { + return; + } + + preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $url, $matches); + + return $matches[1]; +} \ No newline at end of file