So I got most of the code working, but what I am trying to do, is say someone post the following
This is a great youtube video http://www.youtube.com/watch?v=pkrM7x0_fcI&feature=channel what do you think.

I want it to turn the youtube link to http://www.youtube.com/v/pkrM7x0_fcI but keep the text that they typed so it should look like this
This is a great youtube video http://www.youtube.com/v/pkrM7x0_fcI what do you think.

If anyone can help that would be great...

if (preg_match_all('#(http://www.youtube.com)?/(v/([-|~_0-9A-Za-z]+)|watch\?v\=([-|~_0-9A-Za-z]+)&?.*?)#i',$youtubeinput,$output))
				{
				    foreach ($output[4] AS $video_id) {
					$youtubeinput = "<a class=\"flashvideo\" href=\"http://www.youtube.com/v/".$video_id."\">http://www.youtube.com/v/".$video_id."</a>";
    				}
				}

Recommended Answers

All 4 Replies

Member Avatar for nevvermind
<?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;

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

Member Avatar for nevvermind

but it is also printing out the word

My code? No, it doesn't.

// 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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.