我正在使用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的一行。
发布于 2012-12-18 15:46:21
您正在创建一条语句,在该语句中您自己声明换行符。所以你的变量包含换行符,因为你把它们放在了那里。现在您有两个选择:
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 ))
?>https://stackoverflow.com/questions/13928379
复制相似问题