fix(youtube-facade): improve regex pattern for iframe detection
- Made regex more flexible to handle various attribute orders - Added quick check before regex processing - Added null check for preg_replace_callback result - Supports both single and double quotes in src attribute 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -32,23 +32,26 @@ final class YoutubeFacadeContentFilter
|
|||||||
*/
|
*/
|
||||||
public function filter(string $content): string
|
public function filter(string $content): string
|
||||||
{
|
{
|
||||||
// Match YouTube iframes (with or without video-wrapper)
|
// Check if content has YouTube embeds at all (quick check)
|
||||||
// Pattern handles both youtube.com/embed/ and youtube-nocookie.com/embed/
|
if (strpos($content, 'youtube.com/embed/') === false && strpos($content, 'youtube-nocookie.com/embed/') === false) {
|
||||||
$pattern = '/<div class="video-wrapper">\s*<iframe[^>]*src="https?:\/\/(?:www\.)?(?:youtube\.com|youtube-nocookie\.com)\/embed\/([a-zA-Z0-9_-]+)[^"]*"[^>]*><\/iframe>\s*<\/div>/is';
|
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 = '/<iframe\s+[^>]*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];
|
$videoId = $matches[1];
|
||||||
return $this->renderer->render($videoId);
|
return $this->renderer->render($videoId);
|
||||||
}, $content);
|
}, $content);
|
||||||
|
|
||||||
// Also match standalone iframes without wrapper
|
// Return original content if regex failed
|
||||||
$patternStandalone = '/<iframe[^>]*src="https?:\/\/(?:www\.)?(?:youtube\.com|youtube-nocookie\.com)\/embed\/([a-zA-Z0-9_-]+)[^"]*"[^>]*><\/iframe>/is';
|
if ($result === null) {
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
|
||||||
$content = preg_replace_callback($patternStandalone, function ($matches) {
|
return $result;
|
||||||
$videoId = $matches[1];
|
|
||||||
return $this->renderer->render($videoId);
|
|
||||||
}, $content);
|
|
||||||
|
|
||||||
return $content;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user