这很复杂,让我们从示例开始,我们如何使用 mysql 来做到这一点。
mysql> create table test (post text);
Query OK, 0 rows affected (0.12 sec)
mysql> insert into test values ('<img src="...." height="100" width="200">'),('<img src="...." height="300" width="400" >');
Query OK, 2 rows affected (0.07 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from test ;
+--------------------------------------------+
| post |
+--------------------------------------------+
| <img src="...." height="100" width="200"> |
| <img src="...." height="300" width="400" > |
+--------------------------------------------+
现在让我们使用一些函数来进行替换
select
replace
(
post,
concat(
'height="',
substring_index(
substring_index(
post,'height="',-1
),
'"',1
),
'"'
),
concat(
concat(
'height="',substring_index(
substring_index(
post,'height="',-1
),
'"',1
),
'%"'
)
)
) as height from test;
+---------------------------------------------+
| height |
+---------------------------------------------+
| <img src="...." height="100%" width="200"> |
| <img src="...." height="300%" width="400" > |
+---------------------------------------------+
2 rows in set (0.00 sec)
同样的宽度可以替换为
select
replace
(
post,
concat(
'width="',
substring_index(
substring_index(
post,'width="',-1
),
'"',1
),
'"'
),
concat(
concat(
'width="',substring_index(
substring_index(
post,'width="',-1
),
'"',1
),
'%"'
)
)
) as width from test;
+---------------------------------------------+
| width |
+---------------------------------------------+
| <img src="...." height="100" width="200%"> |
| <img src="...." height="300" width="400%" > |
+---------------------------------------------+
2 rows in set (0.01 sec)
最后在更新命令中使用它们
update test set
text =
replace
(
post,
concat(
'height="',
substring_index(
substring_index(
post,'height="',-1
),
'"',1
),
'"'
),
concat(
concat(
'height="',substring_index(
substring_index(
post,'height="',-1
),
'"',1
),
'%"'
)
)
),
text =
replace
(
post,
concat(
'width="',
substring_index(
substring_index(
post,'width="',-1
),
'"',1
),
'"'
),
concat(
concat(
'width="',substring_index(
substring_index(
post,'width="',-1
),
'"',1
),
'%"'
)
)
) ;
mysql> select * from test ;
+----------------------------------------------+
| post |
+----------------------------------------------+
| <img src="...." height="100%" width="200%"> |
| <img src="...." height="300%" width="400%" > |
+----------------------------------------------+
2 rows in set (0.00 sec)