在MySQL表中查找并替换字符串

I wish to update tables in a WordPress database, and I am confident with what I wish to achieve, and it won't do any damage, I am struggling to achieve what I want.

in the post_content field there is:

<img class="aligncenter  wp-image-1603" title="365 Days of Robot, Day 4" alt="365 Days of Robot, Day 41"
src="http://www.silversnaps.co.uk/wp-content/uploads/041-365-Days-of-Robot.jpg"
/>

Now I wish to replace the img class with

<img class="aligncenter  wp-image-1603 img-responsive"

I know how to update the table - but I can't figue out how to match what I wish to update. I would like to add the class 'img-responsive' at tne end of the class.

UPDATE your_table SET post_content = REPLACE(post_content, '<img
class="%"', '<img Class"% img-responsive') WHERE post_content LIKE
'%<img class"%"'

If you're just doing this one field how about

UPDATE your_table 
SET post_content = REPLACE(post_content, '<img class="%"', '<img class="% img-responsive') WHERE post_content LIKE '%aligncenter  wp-image-1603%'

Or if you need something broader, such as all content images, how about

UPDATE your_table 
SET post_content = REPLACE(post_content, '<img class="', '<img class="img-responsive ') WHERE post_content LIKE '%<img class="%'

That will assume of course the class is the first attribute of all your image tags.

By the way: be sure you run any scripts through a SELECT first to make sure you are fetching what you want! :)

I don't think you can use wildcard % in your REPLACE. Try this sqlFiddle

UPDATE your_table SET post_content = REPLACE(post_content,SUBSTRING_INDEX(SUBSTRING_INDEX(post_content,'" title="',1),'<img class="',-1),
                                                   CONCAT(SUBSTRING_INDEX(SUBSTRING_INDEX(post_content,'" title="',1),'<img class="',-1),' img-responsive'))
WHERE post_content like '%<img class="%"'