【问题标题】:PHP Tokenizer multiline issuePHP Tokenizer 多行问题
【发布时间】:2012-12-18 07:37:34
【问题描述】:

我正在使用 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 的单行。

【问题讨论】:

  • 不是 100% 确定您的问题,但如果您只是需要一些东西让您更清楚地阅读,为什么不使用 <<<EOFEOF

标签: php token tokenize lexical


【解决方案1】:

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

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 ))
?>

【讨论】:

  • 它不仅仅是我的代码。有很多开发人员,所以第一个选项对我来说不是解决方案。对于第二个,其实这是我的观点,我如何识别这条线是多线,这样我就可以消除 chr(10), chr(13)。
  • 如果你想识别它,你可以使用 substr_count 或 strpos 来查看它是否在 chr(10) 或 chr(13) 上返回 >0(或 >=0 用于 strpos)。但你也可以直接更换它。如果它已经是单行,则不会更改任何内容,如果是多行,则将其更改为单行
  • Hugo 你将如何通过 token_get_all 获得完整的线路。我没有得到这部分......
  • 谢谢,我有一个包含这个查询和数组的 php 代码的文件。我在上面运行了同样的方法,但仍然在多个令牌中获取查询,而不是单行。一旦我手头有一个完整的查询作为单行,您的解决方案就会起作用。这是没有发生的事情。此外,您的代码中的 $c 必须是 sting,我需要数组。
  • 那我不知道你想做什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-27
  • 1970-01-01
相关资源
最近更新 更多