tags:

views:

363

answers:

1

I am writing temporary PHP script to update MySQL database of my vBulletin forum.

Here's what it does. It finds any entry that has a [youtube][/youtube] code. And then it has to replace that code with a link to the youtube video instead.

So, here is an example of what I have to take:

$string = <<<END
Hi everyone! Check out this video that I just found on YouTube!

[youtube]<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/Md1E_Rg4MGQ&amp;hl=en&amp;fs=1&amp;"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/Md1E_Rg4MGQ&amp;hl=en&amp;fs=1&amp;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>[/youtube]
END;

And I have to make that look like this instead:

[URL=http://www.youtube.com/watch/?v=Md1E_Rg4MGQ]http://www.youtube.com/watch/?v=Md1E_Rg4MGQ[/URL]

I'm getting a headache working with the Regex. I don't have enough experience with Regex to figure out what to do.

It has to look something like this:

$string = preg_replace("#\[youtube\]?????\[/youtube\]#i", "[URL=http://www.youtube.com/watch?v=$1]http://www.youtube.com/watch?v=$1[/URL]", $string);

Help Please! ^_^

A: 

Something like this perhaps?

$string = preg_replace('#\[youtube\].*?name="movie" value="(.*?)".*?\[/youtube\]#i', "[URL=$1]$1[/URL]", $string);

Note that this is limited in that it expects a string name="movie" value="????????????????" format precisely - technically, there are other valid HTML constructs with the same meaning but different order, etc. The ideal solution would be to use some sort of DOM parser to grab the value of the movie attribute, but if you know people will always be using that exact format (i.e. if that's copy the copy/paste from youtube always is or the like) then regex can suffice.

Amber
I got it. Thanks :)
stackoverflowUser001