Jump to content

Query for word replacement like in a text editor?


Recommended Posts

I have a forum, and in the db I want to update some bbcode.

 

With the new forum software, when a youtube URL is pasted it embeds. I want to remove the old bbcode, but that will render previous posts unembedded. I have a solution, I just don't know how to do it. I need to do a replace all, similar to that in a text editor.

 

The old bbcode is:

[yt]video_number_here[/yt]

 

In a text editor if I search for [yt] and replace all with http://www.youtube.com/watch?v=

I get:

http://www.youtube.com/watch?v=video_number_here[/yt]

 

Now to remove the end tag, I search for [/yt] and replace all with the replacement field blank, and I get:

http://www.youtube.com/watch?v=video_number_here

 

Well that's how it would be done in a text editor, but I have no idea how I would do this in my db. Are there any queries that could do this? And please let me know if they are case sensitive, for the [yt].

 

Thanks for any help.

 

 

MySQL REPLACE() function. As far as case-sensitivity, I'm not sure, so you'll have to run some tests.

 

UPDATE `table` SET `field` = REPLACE(`field`, '[yt]', 'http://www.youtube.com/watch?v='), `field` = REPLACE(`field`, '[/yt]', '')

Awesome, so this is possible!!

 

I could also replace with tags, I would just need to put that youtube URL after the first opening tag, and then replace the closing tag.

 

So if I did that instead, the code would be:

 

UPDATE `table` SET `field` = REPLACE(`field`, '[yt]', '[media]http://www.youtube.com/watch?v='), `field` = REPLACE(`field`, '[/yt]', '[/media]')

 

And this:

[yt]video_number_here[/yt]

 

Becomes:

[media]http://www.youtube.com/watch?v=video_number_here

 

Correct? No spacing, or line break issues?

 

Thanks so much. I'll have to test on a test db before trying live.

Yes, that looks right. I dunno about line breaks/spacing issues for certain, though I've never run into any. But as you say, testing it on a dev DB first would definitely be a good idea.

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.