Mysqli_real_escape_string阻止preg_split工作?

时间:2013-02-17 21:56:20

标签: php mysqli preg-split

Example Text: This will be stored in the $_POST['x'] variable
This is sentence one.
This is sentence two.
This is sentence three.

如果我在下面运行此代码,它将返回一个只包含一个元素的数组

$x= mysqli_real_escape_string($db, $_POST['x']);
$y= preg_split("/(\r\n|\n|\r)/", $x);

但如果我在下面运行此代码,它会将其正确地分成所有3个元素。

$x = $_POST['x'];
$y= preg_split("/(\r\n|\n|\r)/", $x);

还有其他人遇到过这种现象吗?为什么会这样?

1 个答案:

答案 0 :(得分:4)

  

http://php.net/mysqli-real-escape-string
  编码的字符是NUL(ASCII 0),\ n,\ r,\,',“和Control-Z。

这意味着换行符变为\\n(LF),\\r(CR)和\\r\\n(CRLF)。因此它们不再匹配正则表达式。

将来,RTM;)