在PHP中修改数据

时间:2011-12-24 07:09:54

标签: php replace preg-match-all

如何修改此数据:

(13, 'µµ+3                    ', 'Miecz +3', 1, 0, 0, 2, 32, 1, 16, '', 100, 0, 14, 4, 0, 15, 1, 0, 0, 0, 7, 22, 0, 0, 0, 0, 0, 15, 19, 13, 15, 21, -1, -1, -1, -1, -1, -1, 0, 1, 0),
(14, 'µµ+4                    ', 'Miecz +4', 1, 0, 0, 2, 32, 1, 16, '', 100, 0, 15, 5, 0, 15, 1, 0, 0, 0, 7, 22, 0, 0, 0, 0, 0, 15, 19, 13, 15, 28, -1, -1, -1, -1, -1, -1, 30, 1, 0),
(15, 'µµ+5                    ', 'Miecz +5', 1, 0, 0, 2, 32, 1, 16, '', 100, 0, 16, 6, 0, 15, 1, 0, 0, 0, 7, 22, 0, 0, 0, 0, 0, 15, 19, 13, 15, 35, -1, -1, -1, -1, -1, -1, 40, 1, 0),
(16, 'µµ+6                    ', 'Miecz +6', 1, 0, 0, 2, 32, 1, 16, '', 100, 0, 17, 7, 0, 15, 1, 0, 0, 0, 7, 22, 0, 0, 0, 0, 0, 15, 19, 13, 15, 42, -1, -1, -1, -1, -1, -1, 50, 1, 0),
(17, 'µµ+7                    ', 'Miecz +7', 1, 0, 0, 2, 32, 1, 16, '', 100, 0, 18, 8, 0, 15, 1, 0, 0, 0, 7, 22, 0, 0, 0, 0, 0, 15, 19, 13, 15, 49, -1, -1, -1, -1, -1, -1, 65, 1, 0),
(18, 'µµ+8                    ', 'Miecz +8', 1, 0, 0, 2, 32, 1, 16, '', 100, 0, 19, 9, 0, 15, 1, 0, 0, 0, 7, 22, 0, 0, 0, 0, 0, 15, 19, 13, 15, 56, -1, -1, -1, -1, -1, -1, 80, 1, 0),
(19, 'µµ+9                    ', 'Miecz +9', 1, 0, 0, 2, 32, 1, 16, '', 2000, 0, 0, 0, 0, 15, 1, 0, 0, 0, 7, 22, 0, 0, 0, 0, 0, 15, 19, 13, 15, 63, -1, -1, -1, -1, -1, -1, 100, 1, 0),
(20, 'Ŕĺ°Ë+0                  ', 'Dlugi Miecz +0', 1, 0, 0, 2, 32, 1, 16, '', 400, 0, 21, 1, 0, 15, 1, 5, 0, 0, 7, 27, 0, 0, 0, 0, 0, 13, 15, 15, 19, 0, -1, -1, -1, -1, -1, -1, 0, 1, 0),
(21, 'Ŕĺ°Ë+1                  ', 'Dlugi Miecz +1', 1, 0, 0, 2, 32, 1, 16, '', 400, 0, 22, 2, 0, 15, 1, 5, 0, 0, 7, 27, 0, 0, 0, 0, 0, 13, 15, 15, 19, 7, -1, -1, -1, -1, -1, -1, 0, 1, 0),
(22, 'Ŕĺ°Ë+2                  ', 'Dlugi Miecz +2', 1, 0, 0, 2, 32, 1, 16, '', 400, 0, 23, 3, 0, 15, 1, 5, 0, 0, 7, 27, 0, 0, 0, 0, 0, 13, 15, 15, 19, 14, -1, -1, -1, -1, -1, -1, 0, 1, 0),

进入查询,如下:

UPDATE `itemproto`
SET `name` = 'µµ+3'
WHERE `id` = 13;

UPDATE `itemproto`
SET `name` = 'µµ+4'
WHERE `id` = 14;

...

UPDATE `itemproto`
SET `name` = 'Ŕĺ°Ë+2'
WHERE `id` = 22;

...等。有数百个数据,我刚给你一点切,因为文件重量是一个不错的MB。但好吧,让我解释一下,我想达到什么目的:

的Fe。有一条这样的线......

(13, 'µµ+3                    ', 'Miecz +3', 1, 0, 0, 2, 32, 1, 16, '', 100, 0, 14, 4, 0, 15, 1, 0, 0, 0, 7, 22, 0, 0, 0, 0, 0, 15, 19, 13, 15, 21, -1, -1, -1, -1, -1, -1, 0, 1, 0),

然后脚本应该将上面的行重写为一个查询:

UPDATE `itemproto`
SET `name` = 'µµ+3'
WHERE `id` = 13;

您只需要从每一行获取以下信息即可构建查询:

(13, 'µµ+3 ' ,等....

我认为可以使用preg_replacepreg_match完成,但我不知道正则表达式。

无论如何,如果你仍然没有得到它,我会再试一次解释:

我有这样的数百行(随机名称,数字):

(13, 'µµ+3                    ', 'Miecz +3', 1, 0, 0, 2, 32, 1, 16, '', 100, 0, 14, 4, 0, 15, 1, 0, 0, 0, 7, 22, 0, 0, 0, 0, 0, 15, 19, 13, 15, 21, -1, -1, -1, -1, -1, -1, 0, 1, 0),

现在脚本应该grep这个信息:(13, 'µµ+3,并根据这些信息,应该构建以下查询:

UPDATE `itemproto`
SET `name` = 'µµ+3'
WHERE `id` = 13;

就是这样。

我知道它有点复杂,但是如你所知,一切都可以用PHP完成。所以这就是我使用它的原因,但是,如果你仍然不理解这个问题,随时发表评论,我会回复!

2 个答案:

答案 0 :(得分:1)

一个有点简单的解决方案:

$data = file("path/to/file.txt"); // returns an array of the lines
foreach($data as $key => $val){
    $temp = explode(',', $val);
    $id = trim(substr($temp[0], 1));
    $name = trim($temp[1]);
    $query = "UPDATE `itemproto`
        SET `name` = '$name'
        WHERE `id` = $id;";
    // Do work here
}

答案 1 :(得分:1)

这是基于Charlie Somerville的答案,但只准备一次PDOStatement对象。结果会明显加快。

$prepped = $pdo -> prepare ("UPDATE some_table SET name = ? WHERE id = ?");
foreach ($rows as $row) {
    // I'm guessing the first element of each row is the id, and the second is the name
    list ($id, $name) = $row;
    // There should be some error checking here, but for the sake of simplicity I've left it out.
    $prepped -> execute (array ($id, $name));
}