mysql - 用通配符替换字符串

标签 mysql database wordpress replace

我正在将我管理的网站从自定义 CMS 迁移到 Wordpress,我发现图像标签上的某些属性一旦在 WP 环境中显示就会出现问题。要解决此问题,我需要删除内联到 wp_posts 表的 post_content 列中每个图像标签的高度属性。

从数据库中的原始值开始,我想要以下内容:

<img src="http://example.com/img/20150823_image.jpg" style="width: 730px; height: 730px;" />

成为:

<img src="http://example.com/img/20150823_image.jpg" style="width: 730px;" />

因此,从本质上讲,我需要删除“height: 730px;”部分。它是特定于图像的,因此在这种情况下它是 730,但在另一种情况下它可能是 1500、447、80 等。

我试图看看我是否可以使用“%”作为通配符,但这似乎不起作用...

UPDATE wp_posts SET post_content = REPLACE(post_content,' height: %px;','');

任何帮助将不胜感激,因为我宁愿不必手动浏览数千行来删除这些行。

最佳答案

您可以使用一个函数来进行文本解析:

create function f_strip_height( in_s text ) returns text
begin

declare v_start int;
declare v_end int;
declare v_height text;

select locate(' height:', in_s ) into v_start;
if (v_start>0) then

  select locate( 'px;', substring( in_s, v_start)  ) into v_end;

  select trim(substring(substring( in_s, v_start, v_end+2), 9)) into v_height;

  if (v_end>0 and concat(cast(v_height as unsigned), 'px;' =  v_height)) then
    return concat(substring( in_s, 1, v_start-1), substring( in_s, v_start+v_end+2));
  end if;
end if;

return in_s;
end

然后使用函数:

UPDATE wp_posts SET post_content = f_strip_height(post_content);

关于mysql - 用通配符替换字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43375396/

相关文章:

mysql - 在 MVC 中添加列以连接表和有效负载

mysql - 如何计算每半小时的消息数?

php - 使用 mysql 和 php 从 HTML 表中删除一条记录

database - 带有游标的sqlite3执行完整性错误

wordpress - woocommerce - 在插件中包含客户完成订单电子邮件模板

javascript - jQuery 不选择元素 (Wordpress)

mysql - d3js : data/column required for time series chart with dropdown list

MySQL 数据库优化最佳实践

java - 重复值未插入到 sqlite 数据库中

php - 使用 PHP 仅在某些页面上强制使用 SSL