douxian6086 2014-01-14 18:09
浏览 37

在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"%"'
  • 写回答

2条回答 默认 最新

  • douchen2025 2014-01-14 20:14
    关注

    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! :)

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP