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*** Link to comment https://forums.phpfreaks.com/topic/207366-replace-youtube-link-with-embed-video/ Share on other sites More sharing options...
ajicles Posted July 10, 2010 Share Posted July 10, 2010 $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 https://forums.phpfreaks.com/topic/207366-replace-youtube-link-with-embed-video/#findComment-1084188 Share on other sites More sharing options...
Zane Posted July 10, 2010 Share Posted July 10, 2010 Try this $yObj = "\n \n \n \n \n \n"; $yLink = preg_replace("#http://www.youtube.com/watch?v=([a-zA-Z0-9]+)&#", $yObj); Link to comment https://forums.phpfreaks.com/topic/207366-replace-youtube-link-with-embed-video/#findComment-1084197 Share on other sites More sharing options...
AviNahum Posted July 11, 2010 Author Share Posted July 11, 2010 first, thanks! but you missed the part where i sad that the link can be in a middle of a paragraph... i need to catch the youtube link in the paragraph and replace only the link with the embed code... Link to comment https://forums.phpfreaks.com/topic/207366-replace-youtube-link-with-embed-video/#findComment-1084310 Share on other sites More sharing options...
.josh Posted July 11, 2010 Share Posted July 11, 2010 $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 https://forums.phpfreaks.com/topic/207366-replace-youtube-link-with-embed-video/#findComment-1084327 Share on other sites More sharing options...
ZachMEdwards Posted July 11, 2010 Share Posted July 11, 2010 Crayon your pattern isn't fully correct. Youtube can have _ and - in the video ID's. I'd use this as your pattern: '/v=([^&]+)/' Link to comment https://forums.phpfreaks.com/topic/207366-replace-youtube-link-with-embed-video/#findComment-1084349 Share on other sites More sharing options...
.josh Posted July 11, 2010 Share Posted July 11, 2010 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 https://forums.phpfreaks.com/topic/207366-replace-youtube-link-with-embed-video/#findComment-1084448 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.