我们正在改变画廊软件从共享到页,需要改变他们在博客文章中使用的id。现在,post内容的短手代码为:
gallery_id="14629“图库类型= 0,等等
我们需要把它改为:
美食家gallery_id="14629“
因此,我需要更改图库的名称,然后去掉方括号中的其余文本,而不更改方括号之外的文本。
对于如何使用MySQL,是否有其他人有其他的建议?
谢谢
发布于 2019-02-15 16:20:27
最简单的方法就是编写脚本并更新所有内容。除非你有数以百万计的条目,否则这将是更容易的方法。只需与MYSQL连接,将所有行放入一个数组中,然后循环每次迭代并进行更新。
<?php
// Get all results in mysql sample data underneath
$input_lines = array(
array(
"id" => 1,
"line"=>'[shareprints gallery_id="14629" gallerytype = 0, etc]'
)
);
// Loop each data
foreach($input_lines as $l) {
// Replace with new format
preg_match_all('/\[[a-zA-Z]*\ ([^\ ]*)[^]]*/', $l['line'], $output_array);
$input_lines = "[testname ". $output_array[1][0]."]"; // Change with new name here
// var_dump($input_lines);
// Update mysql with ID (l['id'] here)
}
?>发布于 2019-02-15 18:59:47
这在一定程度上取决于您的数据库服务器,它支持什么,但是这里还有一个只使用数据库的方法:
> SELECT REGEXP_REPLACE('my text before [shareprints some_othertype gallery_id="14629" gallerytype = 0, etc] my text [shareprints some_othertype gallery_id="1" gallerytype = 0, etc] my text after',
'\\[shareprints [^\\]]*gallery_id="([0-9]+)"[^\\]]*\\]',
'\[foogallery gallery_id="\\1"]');
+--------------------------------------------------------------------------------------------------+
| REGEXP_REPLACE(...) |
+--------------------------------------------------------------------------------------------------+
| my text before [foogallery gallery_id="14629"] my text [foogallery gallery_id="1"] my text after |
+--------------------------------------------------------------------------------------------------+需要注意的是不要让regexp太贪婪,这就是为什么使用[^\\]]* (而不是.*)来捕获标记中不需要的东西(而不是在标记之外)。反向引用(括号)用于应该转移的内容,然后在替换模式中使用相应的\N (N表示数字)。
那么,当您对regexp满意时,可以使用
UPDATE mytable
SET mycol=REGEXP_REPLACE(mycol,'myregexp','myreplacement')
WHERE <my condition in case I do not want to update all rows>..or类似于使数据库忙碌一段时间;)
https://stackoverflow.com/questions/54712943
复制相似问题