PHP - how to create a newline character?

白昼怎懂夜的黑 提交于 2019-11-26 01:31:25

问题


In PHP I am trying to create a newline character:

echo $clientid;
echo \' \';
echo $lastname;
echo \' \';
echo \'\\r\\n\';

Afterwards I open the created file in Notepad and it writes the newline literally:

1 John Doe\\r\\n 1 John Doe\\r\\n 1 John Doe\\r\\n

I have tried many variations of the \\r\\n, but none work. Why isn\'t the newline turning into a newline?


回答1:


Only double quoted strings interpret the escape sequences \r and \n as '0x0D' and '0x0A' respectively, so you want:

"\r\n"

Single quoted strings, on the other hand, only know the escape sequences \\ and \'.

So unless you concatenate the single quoted string with a line break generated elsewhere (e. g., using double quoted string "\r\n" or using chr function chr(0x0D).chr(0x0A)), the only other way to have a line break within a single quoted string is to literally type it with your editor:

$s = 'some text before the line break
some text after';

Make sure to check your editor for its line break settings if you require some specific character sequence (\r\n for example).




回答2:


For platform independent line ending you can use the predefined PHP_EOL constant, as in:

echo $clientid, ' ', $lastname, PHP_EOL;



回答3:


The "echo" command in PHP sends the output to the browser as raw html so even if in double quotes the browser will not parse it into two lines because a newline character in HTML means nothing. That's why you need to either use:

echo [output text]."<br>";

when using "echo", or instead use fwrite...

fwrite([output text]."\n");

This will output HTML newline in place of "\n".




回答4:


Use the constant PHP_EOL to get the right character no matter the platform.

http://us3.php.net/manual/en/reserved.constants.php

A simple usage example:

<?php
$data = 'First line' . PHP_EOL .
        'Second line' . PHP_EOL .
        'Third line';

file_put_contents("filename.txt", $data);
?>



回答5:


Strings between double quotes "" interpolate, meaning they convert escaped characters to printable characters.

Strings between single quotes '' are literal, meaning they are treated exactly as the characters are typed in.

You can have both on the same line:

echo '$clientid $lastname ' . "\r\n";
echo "$clientid $lastname \r\n";

outputs:

$clientid $lastname
1 John Doe



回答6:


You should use this:

"\n"

You also might wanna have a look at PHP EOL.




回答7:


Actually \r\n is for the html side of the output. With those chars you can just create a newline in the html code to make it more readable:

echo "<html>First line \r\n Second line</html>";

will output:

<html>First line
Second line</html>

that viewing the page will be:

First line Second line

If you really meant this you have just to fix the single quote with the "" quote:

echo "\r\n";

Otherwise if you mean to split the text, in our sample 'First line' and 'Second line' you have to use the html code: <br />:

First line<br />Second line

that will output:

First line
Second line

Also it would be more readable if you replace the entire script with:

echo "$clientid $lastname \r\n";



回答8:


w3school offered this way:

echo nl2br("One line.\n Another line.");

by use of this function you can do it..i tried other ways that said above but they wont help me..




回答9:


For some reason, every single post asking about newline escapes in PHP fails to mention the case that simply inserting a newline into single-quoted strings will do exactly what you think:

ex 1.

 echo 'foo\nbar';

Example 1 clearly does not print the desired result, however, while it is true you cannot escape a newline in single-quotes, you can have one:

ex 2.

 echo 'foo
 bar';

Example 2 has exactly the desired behavior. Unfortunately the newline that is inserted is operating system dependent. This usually isn't a problem, as web browsers/servers will correctly interpret the newline whether it is \r, \r\n, or \n.

Obviously this solution is not ideal if you plan to distribute the file through other means then a web browser and to multiple operating systems. In that case you should see one of the other answers.

note: using a feature rich text editor you should be able to insert a newline as a binary character(s) that represents a newline on a different operating system than the one editing the file. If all else fails, simply using a hex editor to insert the binary ascii character would do.




回答10:


Use chr (13) for carriage return and chr (10) for new line

echo $clientid;
echo ' ';
echo $lastname;
echo ' ';
echo chr (13). chr (10);



回答11:


Use the PHP nl2br to get the newlines in a text string..

$text = "Manu is a good boy.(Enter)He can code well.

echo nl2br($text);

Result.

Manu is a good boy.

He can code well.




回答12:


I have also tried this combination within both the single quotes and double quotes. But none has worked. Instead of using \n better use <br/> in the double quotes. Like this..

$variable = "and";
echo "part 1 $variable part 2<br/>";
echo "part 1 ".$variable." part 2";



回答13:


For any echo statements, I always use <br> inside double quotes.




回答14:


You Can Try This.
<?php
   $content = str_replace(PHP_EOL, "<br>", $your_content);
 ?>
 
<p><?php echo($content); ?></p>


来源:https://stackoverflow.com/questions/4238433/php-how-to-create-a-newline-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!