preg_replace()正在取代太多

时间:2016-03-30 13:40:15

标签: php regex parameterized-query

我正在研究一个简单的SQL调试器,它将接受参数化变量并尝试相应地替换它们,这样如果一个SQL有问题,那么我可以将其直接复制+粘贴到我的RDBMS中以处理查询,并希望更快地调试问题。

到目前为止我基本上都有这个,但它取代了太多:

<?php
$sql = "select *
  from table_name
 where comment like :a and
       email = :b and
       status = :c";

$patterns = array();
$patterns[0] = '/:a/';
$patterns[1] = '/:b/';
$patterns[2] = '/:c/';

$replacements = array();
$replacements[0] = "'%that is a nice :b but this one%'";
$replacements[1] = "'monkeyzeus@example.com'";
$replacements[2] = "'active'";

echo preg_replace($patterns, $replacements, $sql);

导致

select *
  from table_name
 where comment like '%that is a nice 'monkeyzeus@example.com' but this one%' and
       email = 'monkeyzeus@example.com' and
       status = 'active'

请注意,位置1的'monkeyzeus@example.com'正从位置0进入:b

我发现了这个问题Can preg_replace make multiple search and replace operations in one shot?,但我无法做出正面或反面,因为我当然不是正则表达式专家。

更新。只是想分享最终产品:

function debug_sql($sql = NULL, $params = NULL)
{
    return (
        $sql !== NULL && is_array($params) && $params ? // $sql and $params is required
        strtr( // Feed this function the sql and the params which need to be replaced
            $sql,
            array_map( // Replace single-quotes within the param items with two single-quotes and surround param in single-quotes
                function($p)
                {
                    return "'".str_replace("'", "''", $p)."'"; // Basic Oracle escaping
                },
                $params
            )
        ) :
        $sql
    );
}

3 个答案:

答案 0 :(得分:2)

来自jeroen的一些富有洞察力的建议:

  

首先用一些永远不会出现的哈希/占位符替换占位符,然后用替换字符串替换它们。

我已经想出了这个,它似乎适用于我的所有测试用例:

<?php
$sql = "select *
  from table_name
 where comment like :a and
       email = :b and
       status = :c and
       something = :bb";

$patterns = array();
$replacements = array();

$patterns[0][0] = '/(:a)\\b/';
$patterns[0][1] = '/(:b)\\b/'; // Use word-boundary to prevent :b from being found in :bb
$patterns[0][2] = '/(:c)\\b/';
$patterns[0][3] = '/(:bb)\\b/';

$replacements[0][0] = str_replace('.', '', uniqid('', TRUE));
$replacements[0][1] = str_replace('.', '', uniqid('', TRUE));
$replacements[0][2] = str_replace('.', '', uniqid('', TRUE));
$replacements[0][3] = str_replace('.', '', uniqid('', TRUE));

$patterns[1][0] = '/('.$replacements[0][0].')\\b/';
$patterns[1][1] = '/('.$replacements[0][1].')\\b/';
$patterns[1][2] = '/('.$replacements[0][2].')\\b/';
$patterns[1][3] = '/('.$replacements[0][3].')\\b/';

$replacements[1][0] = "'%that is a nice :b but this one%'";
$replacements[1][1] = "'monkeyzeus@example.com'";
$replacements[1][2] = "'active'";
$replacements[1][3] = "'another thing'";

$sql = preg_replace($patterns[0], $replacements[0], $sql);
$sql = preg_replace($patterns[1], $replacements[1], $sql);

echo $sql;

这可能失败的唯一方法是用户在处理时查询str_replace('.', '', uniqid('', TRUE))的确切输出。

答案 1 :(得分:2)

这种情况下有一个特殊功能: strtr - 翻译字符或替换子字符串 http://php.net/manual/en/function.strtr.php

<?php

$sql = "select * from table_name where comment like :a and email = :b and status = :c";

$map = [
    ':a' => "'%that is a nice :b but this one%'",
    ':b' => "'monkeyzeus@example.com'",
    ':c' => "'active'"
];

echo strtr($sql, $map);

答案 2 :(得分:1)

没有regexp的替代方法是以递归方式展开/破坏查询:

$sql = "select * from table_name where comment like :a and email = :b and status = :c ";

$patterns = array();
$patterns[0] = ' :a ';
$patterns[1] = ' :b ';
$patterns[2] = ' :c ';

$replacements = array();
$replacements[0] = " '%that is a nice :b but this one%' ";
$replacements[1] = " 'monkeyzeus@example.com' ";
$replacements[2] = " 'active' ";

function replace($substr, $replacement, $subj) {
    if (empty($substr)) {
        return $subj;
    }
    $s = array_shift($substr);
    $r = array_shift($replacement);
    foreach($subj as &$str) {
        $str = implode($r, replace($substr, $replacement, explode($s, $str)));
    }
    return $subj;
}

echo replace($patterns, $replacements, [$sql])[0];