indie Posted February 11, 2011 Share Posted February 11, 2011 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. Link to comment https://forums.phpfreaks.com/topic/227328-query-for-word-replacement-like-in-a-text-editor/ Share on other sites More sharing options...
Pikachu2000 Posted February 11, 2011 Share Posted February 11, 2011 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]', '') Link to comment https://forums.phpfreaks.com/topic/227328-query-for-word-replacement-like-in-a-text-editor/#findComment-1172582 Share on other sites More sharing options...
indie Posted February 11, 2011 Author Share Posted February 11, 2011 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. Link to comment https://forums.phpfreaks.com/topic/227328-query-for-word-replacement-like-in-a-text-editor/#findComment-1172586 Share on other sites More sharing options...
Pikachu2000 Posted February 11, 2011 Share Posted February 11, 2011 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. Link to comment https://forums.phpfreaks.com/topic/227328-query-for-word-replacement-like-in-a-text-editor/#findComment-1172589 Share on other sites More sharing options...
indie Posted February 11, 2011 Author Share Posted February 11, 2011 Awesome, thanks. Link to comment https://forums.phpfreaks.com/topic/227328-query-for-word-replacement-like-in-a-text-editor/#findComment-1172592 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.