AviNahum Posted July 10, 2010 Share Posted July 10, 2010 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*** Quote Link to comment Share on other sites More sharing options...
leewad Posted July 11, 2010 Share Posted July 11, 2010 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.