Question

i am making a bbcode for youtube videos.User can post a video as bbcode eg like [youtube]http://www.youtube.com/watch?v=ihK2pPcDSHM[/youtube]. Next, it will convert it to html code.But instead of video,i want to show also the image of the video. So i do it like this:

$string = preg_replace("~\[yt]http://www.youtube.com/watch\?v=(.*)\[/yt]~Uis","<img src=\"http://img.youtube.com/vi/\\1/0.jpg\" />", $string);

It shows the image, but when somebody puts a url like:

http://www.youtube.com/watch?v=ihK2pPcDSHM&feature=channel

Then the image url becomes http://img.youtube.com/vi/ihK2pPcDSHM&feature=channel1/0.jpg which does not lead to a valid image. I am trying to change the \\1 to ".substr('\\1', 0,11)." but it doesnt have any result.

Any suggestion to solve this? Thanks!

Was it helpful?

Solution

Try a different pattern like:

~\[yt]http://www\.youtube\.com/watch\?v=([a-z0-9-_]+).*?\[/yt]~is

OTHER TIPS

Just tell your regex to stop on the first & character:

$string = preg_replace("~\[yt]http://www.youtube.com/watch\?v=([^\\&]*)\[/yt]~Uis","<img src=\"http://img.youtube.com/vi/\\1/0.jpg\" />", $string);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top