MySQL: Replace substring if string ends in jpg, gif or png

admin

Administrator
Staff member
I'm doing a favor for a friend, getting him off of Blogger and onto a hosted WordPress blog.

The big problem is, with over 1,800 posts, there are a lot of image links to deal with. WordPress has no mechanism to import these automatically, so I'm doing it manually.

I've used wget to download every single image that has ever been linked/embedded on the site. Now I need some help building a MySQL query to change all of the images in the blog to their new address.

For example:

Code:
http://www.externaldomain.com/some/link/to/an/image.jpg

Ought to become:

Code:
http://www.newbloghosting.com/wordpress/wp-content/uploads/legacy/www.externaldomain.com/some/link/to/an/image.jpg

So the condition is, if a string in post_content ends in jpeg, jpg, gif or png, replace:

Code:
http://

with

Code:
http://www.newbloghosting.com/wordpress/wp-content/uploads/legacy/

I know how to do a blanket replace with

Code:
UPDATE wp_posts SET post_content = replace(post_content, 'http://www.old-domain.com', 'http://www.new-domain.com');

But I'm having a hard time figuring out how to accomplish my more nuanced, conditional approach.

Thanks for any guidance you can offer. (Torn between posting here or ServerFault but SO looks like it has plenty of MySQL gurus, so here I am.)