【问题标题】:How are \r \t and \n different from one another? [duplicate]\r \t 和 \n 有什么不同? [复制]
【发布时间】:2024-01-09 07:27:01
【问题描述】:

在下面的代码中,我不知道这些字符在功能上有何不同:\r \t \n。有人对这些有解释或描述吗?

这里是一些示例代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Sorting words in a block of text by length</title>
        <link rel="stylesheet" type="text/css" href="common.css" />
    </head>
    <body>
        <h1>Sorting words in a block of text by length</h1>

<?php

$myText = <<<END_TEXT
But think not that this famous town has 
only harpooneers, cannibals, and 
bumpkins to show her visitors. Not at 
all. Still New Bedford is a queer place. 
Had it not been for us whalemen, that 
tract of land would this day perhaps 
have been in as howling condition as the 
coast of Labrador.
END_TEXT;

echo "<h2>The text:</h2>";
echo "<div style=\"width: 30em;\">$myText</div>";

$myText = preg_replace( "/[\,\.]/", "", $myText );
$words = array_unique( preg_split( "/[ \n\r\t]+/", $myText ) );
usort( $words, create_function( '$a, $b', 'return strlen($a) - strlen($b);
' ) );

echo "<h2>The sorted words:</h2>";
echo "<div style=\"width: 30em;\">";

foreach ( $words as $word ) {
    echo "$word ";
}
echo "</div>";

?>
    </body>
</html>

【问题讨论】:

标签: php escaping character


【解决方案1】:

\n 是换行符

\t 是制表符

\r 用于“返回”

您可以在这里找到更多信息:What is the difference between \r and \n?

【讨论】:

  • 谢谢你,Pablo Lemurr。
【解决方案2】:

\n 符号的字面意思是换行。这将转到下一个新行的开头。

\t 符号表示添加一个制表符(通常为 4 个空格,但根据上下文可以轻松为 2 或 8 个)。

\r 符号不再经常使用。这意味着回车,这意味着转到行首。它与\n 一起使用,以确保即使是“旧”打印机也能到达下一行的开头。

【讨论】: