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

$yturl = str_replace('watch?v=', 'v/', $row['post']);

$str = '<object width="640" height="385"><param name="movie" value="'.$yturl.'&hl=en_US&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'.$yturl.'&hl=en_US&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>';

echo $str;

 

I already made one of these kind of script for a database of youtube videos that are called back using $_GET from the page url and calling that from the database ID number.

 

How this works is it replaces watch?v= with v/ from $row['post'] and puts it into a string called $yturl then replaces the variable $yturl from the string $str with the fixed url into that embed code and calls it back through the echo.

Link to comment
Share on other sites

$yObj = "<object width='480' height='385'>\n
     <param name='movie' value='http://www.youtube.com/v/\\1&hl=en_US&fs=1'></param>\n
     <param name='allowFullScreen' value='true'></param>\n
     <param name=allowscriptaccess' value='always'></param>\n
     <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>\n
</object>\n";
$yLink = preg_replace("#http://www.youtube.com/watch?v=([a-zA-Z0-9]+)is", $yObj,$subject);

Link to comment
Share on other sites

Crayon your pattern isn't fully correct. Youtube can have _ and - in the video ID's. I'd use this as your pattern:

'/v=([^&]+)/'

 

Well I'm not psychic, the OP didn't mention that.  Also I just modified what Zanus already did.  Thanks for pointing that out though.  It is a common occurrence in this forum for people to not fully explain what a string/value can be/contain.  As they say, GIGO.

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.