doofy Posted August 22, 2013 Share Posted August 22, 2013 Hello, I'm new to preg_match & preg_match functions. I'm trying to get my brother to post link within a [pseudo BB code tag], which would then embed itself. I don't want him being able to copy and paste codes blindly. Here is the current regex: function BlogCode($blogcode) { if( !strstr($blogcode, '[') ) { return $blogcode; } // Soundcloud if (preg_match('%(?:soundcloud.com)/(?:tracks/)([0-9]+)%i', $blogcode, $match)) { $track_id = $match[1]; $blogcode = preg_replace("~\\[video]([^\\[]*)\\[/video\\]~i","<br /><center><iframe width='100%' height='166' scrolling='no' frameborder='no' src='https://w.soundcloud.com/player/?url=http://api.soundcloud.com/tracks/$track_id'></iframe></center><br /><br />",$blogcode); } // Youtube if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $blogcode, $match)) { $video_id = $match[1]; $blogcode = preg_replace("~\\[video]([^\\[]*)\\[/video\\]~i","<br /><center><object width='640' height='390'><param name='movie' value='https://www.youtube.com/v/".$video_id."?version=3&autoplay=0'></param><param name='allowScriptAccess' value='always'></param><embed src='https://www.youtube.com/v/".$video_id."?version=3&autoplay=0' type='application/x-shockwave-flash' allowscriptaccess='always' width='640' height='390'></embed></object></center>",$blogcode); } return $blogcode; } My issue is when he is using multiple tags, as it will only initialize the $video_id variable the first time around, as it displays the same video instead of individual youtubeID's What am I doing wrong here? I'm sure it's something goofy, but this is my first time trying to do something like this. Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/281461-preg_match-preg_replace-functions-for-embedding-youtubesoundcloudetc/ Share on other sites More sharing options...
.josh Posted August 22, 2013 Share Posted August 22, 2013 preg_match only matches for and returns the first thing found. preg_match_all will look for all things that match the pattern. Basically the issue is that you are finding and capturing the first id.. and then using preg_replace to replace all of the ids with that first id. The best way to fix this is to skip trying tp preg_match or preg_match_all and incorporate what you have there into preg_replace. Link to comment https://forums.phpfreaks.com/topic/281461-preg_match-preg_replace-functions-for-embedding-youtubesoundcloudetc/#findComment-1446312 Share on other sites More sharing options...
doofy Posted August 22, 2013 Author Share Posted August 22, 2013 Ok, that's what I figured might be the case, but when I tried doing preg_match_all(), I must not have done it correctly with a loop to return the arrays from it. Instead of moving to the next line of text (or potential psuedo code) it would simply return all values of the youtube instances immediately within the function, so it wouldn't be in the order he typed it in. I'll see what I can come up with with preg_replace itself as suggested after I do more research. Thank you for your time thus far. Link to comment https://forums.phpfreaks.com/topic/281461-preg_match-preg_replace-functions-for-embedding-youtubesoundcloudetc/#findComment-1446362 Share on other sites More sharing options...
doofy Posted August 23, 2013 Author Share Posted August 23, 2013 Took your advice, looked into using preg_replace solely. $search = '#(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})#x'; $replace = '<center><iframe width="560" height="315" src="http://www.youtube.com/embed/\\1" frameborder="0" allowfullscreen></iframe></center>'; $embed = preg_replace($search, $replace, $embed ); Figured I'd post the above in case any other noobs came by with a similar misunderstanding of how to get it working properly. Cheers! Link to comment https://forums.phpfreaks.com/topic/281461-preg_match-preg_replace-functions-for-embedding-youtubesoundcloudetc/#findComment-1446409 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.