PHP Tokenizer多行问题

时间:2012-12-18 07:37:35

标签: php token tokenize lexical

我使用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的一行。

1 个答案:

答案 0 :(得分: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)。取决于您的系统可以是其中之一,还是两者兼而有之。请参阅:Is a new line = \n OR \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 ))
?>