Solved

replace youtube links into embed code

Posted on 2010-08-14
6
596 Views
Last Modified: 2012-05-10
there is already a question on this which is not solved.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_26262923.html

$inputText = ' lorem ipsum test test http://www.youtube.com/watch?v=ckRsIy4Zqgo then another youtube link http://www.youtube.com/watch?v=sqiQBGkhE9w&feature=related sometext further';

 I want to replace only youtube links with youtube embed player.

 Output would be smth like this:
lorem ipsum test test EMBED CODE then another youtube link EMBED CODE sometext further

sample EMBED CODE is

<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/BF1QogPioYI&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/BF1QogPioYI&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object
 
0
Comment
Question by:iusephp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 14

Expert Comment

by:Kalpan
ID: 33435703
Here is the example for the appropriate one...

http://embedyoutubevideo.com/php-control-embedded-link-youtube.html

you can select as many as links and than customize with your code...

0
 
LVL 14

Expert Comment

by:Kalpan
ID: 33435719
you can use the loop if you are getting the links from database

foreach($links as $link){

$youtublink = $link['url'];

echo "lorem ipsum test test<br>";

echo '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/'.$youtubelink.'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/BF1QogPioYI&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object';

// if you are storing the whole link to your data base than replace as value="'.$youtubelink.'" above
}
0
 
LVL 1

Author Comment

by:iusephp
ID: 33435749
I am afraid above replies wont really help.

I am allowing users to enter message in a textarea. User can add youtube links (not embed code).
The youtube links in the message needs to be convereted to youtube embed codes in serverside php/

Its all about a string which is having some text and youtube links mixed.
Youtube links needs to be replaced to EMBED CODE. So when the message is rendered back it will be message + youtube videos
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 8

Expert Comment

by:ropenner
ID: 33437869
This snippet works as it stands.  I'm sure there is a more efficient way to do it using php built-in functions but this is readable and easy to understand.

good luck.
<?PHP

# #1 Your cut and paste EMBED code with the "YOU_TUBE_IDENTIFIER" as just some text we will replace later
$EMBED_CODE =<<< endofembedcode
<object width="425" height="344">
<param name="movie" value="http://www.youtube.com/v/YOU_TUBE_IDENTIFIER&hl=en_US&fs=1&"></param>
<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>
<embed src="http://www.youtube.com/v/YOU_TUBE_IDENTIFIER&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344">
</embed>
</object>

endofembedcode;

# #2 the text input by the user
$inputText = ' lorem ipsum test test http://www.youtube.com/watch?v=ckRsIy4Zqgo then another youtube link http://www.youtube.com/watch?v=sqiQBGkhE9w&feature=related sometext further';


# Now to combine #1 and #2 to match your example of
# lorem ipsum test test EMBED CODE then another youtube link EMBED CODE sometext further

$output = "";
# split your input into words and look for "http://www.youtube.com/watch?v="  which looks to be the common portion of the youtube URL
foreach (explode(" ",$inputText) as $word) {
	# see if youtube.com is in there
	if (strlen($word) == 0) continue;#	print "[$word]<BR>";
	if (preg_match("'http://www.youtube.com/watch'",$word)) { # if we find this url at the beginning of the word in position 0
		list($you_tube_url, $parameters) = explode('?',$word); # split the url by the equal sign to get the video reference identifier
		# put the parameters into name=>value pairs
		$mypairs = nameValue($parameters);

		# use the 'v' parameter
		$temp = preg_replace("/YOU_TUBE_IDENTIFIER/","{$mypairs{'v'}}",$EMBED_CODE); # insert the video identifier into the embed code above
		$output .= "$temp ";
	} else {
		$output .= "$word ";
	}
}

print "$output";

function nameValue($parameters="") {
	foreach (explode('&',$parameters) as $pairs) {
		list($name,$value) = explode('=',$pairs);
		$newpairings{$name} = $value;
	}
	return $newpairings;
}
?>

Open in new window

0
 
LVL 8

Expert Comment

by:ropenner
ID: 33437888
comments in the code didn't match the code so I'm reposting the code so it is more understandable.
<?PHP

# #1 Your cut and paste EMBED code with the "YOU_TUBE_IDENTIFIER" as just some text we will replace later
$EMBED_CODE =<<< endofembedcode
<object width="425" height="344">
<param name="movie" value="http://www.youtube.com/v/YOU_TUBE_IDENTIFIER&hl=en_US&fs=1&"></param>
<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>
<embed src="http://www.youtube.com/v/YOU_TUBE_IDENTIFIER&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344">
</embed>
</object>

endofembedcode;

# #2 the text input by the user
$inputText = ' lorem ipsum test test http://www.youtube.com/watch?v=ckRsIy4Zqgo then another youtube link http://www.youtube.com/watch?v=sqiQBGkhE9w&feature=related sometext further';


# Now to combine #1 and #2 to match your example of
# lorem ipsum test test EMBED CODE then another youtube link EMBED CODE sometext further

$output = "";
# split your input into words and look for "http://www.youtube.com/watch?v="  which looks to be the common portion of the youtube URL
foreach (explode(" ",$inputText) as $word) {
	# see if youtube.com is in there
	if (strlen($word) == 0) continue;#	print "[$word]<BR>";
	if (preg_match("'http://www.youtube.com/watch'",$word)) { # if we find this url in the $word
		list($you_tube_url, $parameters) = explode('?',$word); # split the url by the question mark '?' to get the url parameters
		# put the parameters into name=>value pairs
		$mypairs = nameValue($parameters);

		# use the 'v' parameter
		$temp = preg_replace("/YOU_TUBE_IDENTIFIER/","{$mypairs{'v'}}",$EMBED_CODE); # insert the video identifier into the embed code where ever YOU_TUBE_IDENTIFIER is found
		$output .= "$temp ";
	} else {
		$output .= "$word ";
	}
}

print "$output";


# splits up URL parameters into name value pairs
function nameValue($parameters="") {
	foreach (explode('&',$parameters) as $pairs) {
		list($name,$value) = explode('=',$pairs);
		$newpairings{$name} = $value;
	}
	return $newpairings;
}
?>

Open in new window

0
 
LVL 8

Accepted Solution

by:
ropenner earned 250 total points
ID: 33437899
image of the output of the above code
output.jpg
0

Featured Post

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question