3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by vinnyBenson
0
<?php

// experiment with different types of youtube links
// the stranger, the better
$string = <<<YOUTUBE
http://www.youtube.com/watch?v=pkrM7x0_fcI&feature=channel http://www.youtube.com/watch?v=U0Wpx1JJkFw&feature=popular http://www.youtube.com/watch?v=4aJnpDtrH-A&feature=related http://www.youtube.com/watch?v=pkrM7x0_fcI&feature=channel http://www.youtube.com/watch?v=pk5M7n0_fcI&feature=channel
YOUTUBE;

$pattern = '#http://www\.youtube\.com/watch\?v=([^\&]+)\&\w+=\w+#';
$replacement = "<a class=\"flashvideo\" href=\"http://www.youtube.com/v/$1\">http://www.youtube.com/v/$1</a><br />";

// overriding the initial $string or you could use some other variable (e.g. $output)
$string = preg_replace($pattern, $replacement, $string);

echo $string;
0

thank you for that, it helped alot, but it is also printing out the word
channel
after the url. It should not show that but the space and text after the URL.

is there away to do an OR function in there so if the user types in http://www.youtube.com/watch?v=pk5M7n0_fcI it still works? I find that it did not work.

Cheers
Russell

0
// Youtube Link

// FIRSTLY IT CHECKS FOR YOUTUBE URLS WITH MORE THAN THE V VARIABLE I.E. http://www.youtube.com/watch?v=2atm6HiZNPU&feature=related
$Text = preg_replace("#http://www\.youtube\.com/watch\?v=([^\&]+)\&\w+=\w+#",'<a href="http://www.youtube.com/watch?v=$1">$1</a>',$Text);

// SECONDLY IT CHECKS FOR YOUTUBE URLS WITH JUST THE V VARIABLE I.E. http://www.youtube.com/watch?v=2atm6HiZNPU
$Text = preg_replace("#(http://www.youtube.com)?/(v/([-|~_0-9A-Za-z]+)|watch\?v\=([-|~_0-9A-Za-z]+)&?.*?)#",'<a href="http://www.youtube.com/watch?v=$4">$4</a>',$Text);

This is the exact code from my bb.php page, and it works for me, it has done for about 2 years (I think) :D, hope it helps

Edited by vinnyBenson: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.