Jump to content

preg_match \ preg_replace functions for embedding youtube\soundcloud\etc.


Go to solution Solved by doofy,

Recommended Posts

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.

Edited by doofy

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

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.

  • Solution

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!

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.