Reputation: 41
I have researched this problem for almost 5hrs now, the best i could come up with is this code from Stackoverflow post:
<?php
$pattren = '%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/]{11})%i';
?>
what I want to accomplish is that I have a custom made web forums written by someone else, I want to add the ability to watch YouTube videos directly in forum topics, I tried different codes and patterns the best one is the one I have posted up in this post, it matches pretty much any YouTube link, However there is a single problem i cant overcome with this code, the BBCode tag used in the forum is [yt]youtubelink[/yt]
, I tried to prefix the pattern with
<?php
$pattren = '%\[yt\](?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/]{11})[\/yt]%i';
?>
but it just doesn't work there is no error reported whatsoever to indicate there is problem in the pattern
the complete code i used
<?php
$testmessage = "test youtube links [yt]http://www.youtube.com/watch?v=ZCLAwp2HbW4&feature=player_embedded[/yt] more text";
$pattren1 = '%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i';
$pattren2 = '%\[yt\](?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})[\/yt]%i';
echo '<h1>works:-</h1>';
echo preg_replace($pattren1, '<iframe width="350" height="250" sandbox="" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>', $testmessage);
echo '<h1>doesnt work:-</h1>';
echo preg_replace($pattren2, '<iframe width="350" height="250" sandbox="" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>', $testmessage);
?>
also while the first pattern partially works it also leaves some URL query string behind for example the first pattern while it does work and get the video ID it leaves this part in the text message &feature=player_embedded
UPDATE:
I couldn't get it working with straight up preg_replace, so I came up with different way to fix this issue, I'll post the code and it might be of use for somebody so that he doesn't waste 6hrs of his life on this issue again!
<?php
$test = "test youtube links [yt]http://www.youtube.com/watch?v=ZCLAwp2HbW4&feature=player_embedded[/yt] more text";
echo preg_replace_callback('#\[yt\](.*)\[/yt\]#i', function ($matches) {
$regex = '#(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})#i';
preg_match($regex, $matches[1], $found);
if ($found[1])
return '<iframe width="350" height="250" sandbox="" src="http://www.youtube.com/embed/'.$found[1].'" frameborder="0" allowfullscreen></iframe>';
}, $test);
?>
This code works on PHP 5.3 to make it work on < PHP5.3 move the anonymous function to separate function.
Upvotes: 1
Views: 1249
Reputation: 699
If this is actually the solution, you're going to slap yourself! You've missed the escape from the square brackets at the end: [/yt]
should be \[/yt\]
Upvotes: 1