0

我正在使用 token_get_all 开发一个工具。我陷入了在 php 代码中有以下查询的情况

$sql = "UPDATE `key_values` SET
                `Value_Content` = '" . $this->db->escape($revisionValues['value']) . "',
                `Comments` = '" . $this->db->escape($revisionValues['comment']) . "',
                `Is_Active` = '" . $this->db->escape($revisionValues['actstate']) . "',
                `Is_Modified`='1'
                WHERE
                `Key_Value`='" . $candidateKey['key'] . "'
                AND `Email_Template`='" . $candidateKey['template'] . "'
                AND `Locale_ID`='" . $candidateKey['locale'] . "'";

和另一个代码

$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

我想把它当作一条线。如上所述,我无法检测多行代码中的行尾。有什么方法可以检测到。我需要一些标识符来告诉我这个多行 sql 查询是 php 的单行。

4

1 回答 1

0

您正在制作一个声明,您自己在其中声明换行符。所以你的变量包含换行符,因为你把它们放在那里。现在你有两个选择:

1:不要放入换行符

$sql = "UPDATE `key_values` SET ".
            "`Value_Content` = '" . $this->db->escape($revisionValues['value']) . "', ".
            "`Comments` = '" . $this->db->escape($revisionValues['comment']) . "', ".
            "`Is_Active` = '" . $this->db->escape($revisionValues['actstate']) . "', ".
            "`Is_Modified`='1' ".
            "WHERE ".
            "`Key_Value`='" . $candidateKey['key'] . "' ".
            "AND `Email_Template`='" . $candidateKey['template'] . "' ".
            "AND `Locale_ID`='" . $candidateKey['locale'] . "'";

2:删除后

$sql = "UPDATE `key_values` SET
            `Value_Content` = '" . $this->db->escape($revisionValues['value']) . "',
            `Comments` = '" . $this->db->escape($revisionValues['comment']) . "',
            `Is_Active` = '" . $this->db->escape($revisionValues['actstate']) . "',
            `Is_Modified`='1'
            WHERE
            `Key_Value`='" . $candidateKey['key'] . "'
            AND `Email_Template`='" . $candidateKey['template'] . "'
            AND `Locale_ID`='" . $candidateKey['locale'] . "'";
$sql = str_replace(array(chr(10), chr(13)), '', $sql);

所以检测一个换行符是检查 chr(10) 或 chr(13)。根据您使用的系统,可能是其中之一,也可能是两者。请参阅:换行符 = \n 还是 \r\n? ( \r=chr(13)& \n=chr(10))

更新

如果要从 token_get_all() 返回单行字符串,可以使用:

<?php
$c = str_replace(array("\n","\r"), '', print_r(token_get_all('<?php echo; ?>'), true));
print $c;
// token_get_all() returns an array
// print_r(array, true) prints the array and the true param makes it return the output as a string
// replace the newline chars with nothing to make it single line

//single line output:
//Array( [0] => Array ( [0] => 372 [1] => 1 ) [1] => Array ( [0] => 316 [1] => echo [2] => 1 ) [2] => ; [3] => Array ( [0] => 375 [1] => [2] => 1 ) [4] => Array ( [0] => 374 [1] => ?> [2] => 1 ))
?>
于 2012-12-18T07:46:21.283 回答