954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Preg_Replace and match YouTube link

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>";
    				}
				}
russellharrower
Newbie Poster
2 posts since Sep 2010
Reputation Points: 10
Solved Threads: 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>";

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

echo $string;
nevvermind
Light Poster
37 posts since Sep 2010
Reputation Points: 11
Solved Threads: 15
 

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

russellharrower
Newbie Poster
2 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 
but it is also printing out the word


My code? No, it doesn't.

nevvermind
Light Poster
37 posts since Sep 2010
Reputation Points: 11
Solved Threads: 15
 
// 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

vinnyBenson
Newbie Poster
1 post since Apr 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: