diff --git a/Public/YoutubeFacade/Infrastructure/Services/YoutubeFacadeContentFilter.php b/Public/YoutubeFacade/Infrastructure/Services/YoutubeFacadeContentFilter.php index 32f8b759..915a72aa 100644 --- a/Public/YoutubeFacade/Infrastructure/Services/YoutubeFacadeContentFilter.php +++ b/Public/YoutubeFacade/Infrastructure/Services/YoutubeFacadeContentFilter.php @@ -32,23 +32,26 @@ final class YoutubeFacadeContentFilter */ public function filter(string $content): string { - // Match YouTube iframes (with or without video-wrapper) - // Pattern handles both youtube.com/embed/ and youtube-nocookie.com/embed/ - $pattern = '/
\s*]*src="https?:\/\/(?:www\.)?(?:youtube\.com|youtube-nocookie\.com)\/embed\/([a-zA-Z0-9_-]+)[^"]*"[^>]*><\/iframe>\s*<\/div>/is'; + // Check if content has YouTube embeds at all (quick check) + if (strpos($content, 'youtube.com/embed/') === false && strpos($content, 'youtube-nocookie.com/embed/') === false) { + return $content; + } - $content = preg_replace_callback($pattern, function ($matches) { + // Pattern to match YouTube iframes + // Handles: youtube.com/embed/ and youtube-nocookie.com/embed/ + // More flexible: allows any attributes in any order, whitespace variations + $pattern = '/]*src=["\']https?:\/\/(?:www\.)?(?:youtube\.com|youtube-nocookie\.com)\/embed\/([a-zA-Z0-9_-]+)[^"\']*["\'][^>]*>\s*<\/iframe>/is'; + + $result = preg_replace_callback($pattern, function ($matches) { $videoId = $matches[1]; return $this->renderer->render($videoId); }, $content); - // Also match standalone iframes without wrapper - $patternStandalone = '/]*src="https?:\/\/(?:www\.)?(?:youtube\.com|youtube-nocookie\.com)\/embed\/([a-zA-Z0-9_-]+)[^"]*"[^>]*><\/iframe>/is'; + // Return original content if regex failed + if ($result === null) { + return $content; + } - $content = preg_replace_callback($patternStandalone, function ($matches) { - $videoId = $matches[1]; - return $this->renderer->render($videoId); - }, $content); - - return $content; + return $result; } }