Jump to content

replace youtube link with embed video


AviNahum

Recommended Posts

i trying to replace a simple youtube link with the embed video, for example:

this:

http://www.youtube.com/watch?v=iabgQJLqTD4&playnext_from=TL&videos=0pS3nK3dxJs&feature=sub

will be replaced with:

<object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/iabgQJLqTD4&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/iabgQJLqTD4&hl=en_US&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>

 

 

so here is my code:

		$row['post'] = preg_replace(
						"#(.+?)http://www.youtube.com/watch?v=[a-zA-Z0-9._-](.+?)#is",
						'<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/\\1&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/\\1&hl=en_US&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>',
						$row['post']
					);

 

it does not work...

i dont really good at regular expressions so i dont really know what i did there... i just played with that...

the most important thing is that the youtube link can be i a middle of a paragraph  like this:

some text here

some more text

http://www.youtube.com/watch?v=iabgQJLqTD4&playnext_from=TL&videos=0pS3nK3dxJs&feature=sub

some text here

some more text

 

any ideas?

Thanks in advance!

 

***sorry for poor english***

 

 

Link to comment
Share on other sites

An easy way is first to split the string to give you the value of the video link then just add it to other section

 

[code=php:0]
<?php

// link of video
$link = 'http://www.youtube.com/watch?v=iabgQJLqTD4&playnext_from=TL&videos=0pS3nK3dxJs&';


//remove beginning of string
$link = str_replace("http://www.youtube.com/watch?v=", "", $link);


// split the string into sections from '&'
$video_value = explode("&", $link);

// value of video link is first array
$video_value = $video_value[0];


echo $video_value;

$new_video_url= '<object width="640" height="385">
<param name="movie" value="http://www.youtube.com/v/'.$video_value.'&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/'.$video_value.'&hl=en_US&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385">
</embed></object>
';

?>

[/code]

 

Will be short way i`m sure but this one works

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.