我正在将我管理的网站从自定义 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/