php 和特殊字符

php and special characters

在PHP,当我写

$string="\thello world\t"
echo trim($string);

这输出 hello world。但是,当我在 HTML 表单输入字段中输入相同的字符串并向服务器输入 post 时,行为会发生变化。

echo trim($_POST["string"];

以上代码输出\thello world\t

有什么区别?

这是因为双引号内的 \t 是一个扩展为文字制表符的 escape sequence。但是,当您在输入字段中键入 \t 时,它会作为文字字符 \t.

发布

在此示例中,此代码。

echo trim($_POST["string"];

类似于此代码。

$string='\thello world\t'
echo trim($string);

转义序列不会在单引号声明的字符串内展开。

如果您需要从提交的字符串中扩展转义序列的功能,this question 提供了一些技巧。