diff --git a/filter.php b/filter.php
index 62d28f43b484f14c181d152de183562b7f2f0fd7..e5c3c44b9ee8b420f7cbf21238afe52ed7f9948e 100644
--- a/filter.php
+++ b/filter.php
@@ -81,6 +81,9 @@ class filter_opencast extends moodle_text_filter {
                     $height = false;
 
                     foreach ($matches as $match) {
+                        if (empty(trim($match))) {
+                            continue;
+                        }
                         // Check if the match is a video tag.
                         if (substr($match, 0, 6) === "<video") {
                             $video = true;
@@ -163,7 +166,7 @@ class filter_opencast extends moodle_text_filter {
                                 }
 
                                 // Replace video tag.
-                                $text = preg_replace('/<video(?:(?!<\/video>).)*?' . preg_quote($match, '/') . '.*?<\/video>/',
+                                $text = preg_replace('/<video(?:(?!<\/video>).)*?' . preg_quote($match, '/') . '.*?<\/video>/s',
                                     $newtext, $text, 1);
                             }
                             $width = $height = false;