为什么去掉phalcon中的"\n","\r","\t"?

why remove "\n","\r","\t" in phalcon?

我发现在 phalcon-tool 创建的 muilti-module 中有这些代码:

$application = new Application($di);
echo str_replace(["\n","\r","\t"], '', $application->handle()->getContent());

为什么要删除“\n”、“\r”、“\t”?

这是为了用单个 space 替换换行符、换行符和制表符,以便可以将应用程序的详细信息作为单行写入 STD_OUT,不换行。

正如 Spangen 所指出的,这些是一些特殊 'whitespace' 字符的转义序列。

link 这里有关于它们的更多信息:http://us3.php.net/manual/en/language.types.string.php#language.types.string.syntax.double

现在,重要说明:要使它们按 PHP 中的预期工作,您需要将它们括在双引号中,例如:"\t", "\n"。单引号不会产生这些转义序列的预期效果:'\t', '\n'.

为了进一步说明这一点,您可以 运行 此代码并查看结果。 运行 来自控制台的 php myfile.php 可能会导致一些视觉故障,并且 运行 在浏览器中使用它需要您查看 'source code'.

echo "Let's test... ";
echo "Because no new line characters were added, this sentence will be printed in the same line as the previous phrase.";
echo "\n";
echo "But now a new line was added, by typing \n enclosed in double quotes.";
echo "\n";
echo "Now, let's add a tab, between the next two words: hello \t there.";
echo "\n";
echo "Now, let's add a carriage return, which will 'force' the 'cursor' in this string to move to the beginning, thus 'splitting' this string into two. Adding it now: \r There, I just added it before this last sentence.";
echo "\n";
echo 'Finally, these special characters will not work as intended if we just enclose the string with single quotes, as done in this string: \n \t \r';

这个link这里有更多关于换行和回车的区别的信息returns:


所以是的,在您发布的原始代码中,此人使用 str_replace() 删除了那些特殊字符,因为它们往往会在控制台中产生 'visual glitches'(意外的格式错误输出)。