php \r \t 和 \n 有什么不同?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15423001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How are \r \t and \n different from one another?
提问by Preston blah
In the code below, I don't know how these characters are different functionally from one another: \r \t \n. Does anyone have an explanation or description for these?
在下面的代码中,我不知道这些字符在功能上有何不同:\r \t \n。有没有人对这些有解释或描述?
Here is some sample code:
下面是一些示例代码:
<!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>
回答by Lemurr
\nis a symbol for new line
\n是新行的符号
\tis a symbol for tab
\t是制表符的符号
and \ris for 'return'
并且\r是为了“回报”
You can find more information here: What is the difference between \r and \n?
您可以在此处找到更多信息:\r 和 \n 之间有什么区别?
回答by Shoe
The \nsymbol means literally new line. This will go to the start of the next new line.
该\n符号的字面意思是换行。这将转到下一个新行的开头。
The \tsymbol means add a tab(which is usually 4 spaces but can easily be 2 or 8 depending on the context).
该\t符号表示添加一个制表符(通常为 4 个空格,但根据上下文可以很容易地为 2 个或 8 个)。
The \rsymbol is no more used that often. It means carriage returnwhich means go to the start of the line. It was used together with \nfor being sure even "old" printers would get to the beginning of the next line.
该\r符号不再经常使用。它的意思是回车,意思是转到行首。它与\n确保即使“旧”打印机也会到达下一行的开头一起使用。

