PHP New Line Not Working [duplicate]

2019-02-17 10:57发布

问题:

Possible Duplicate:
Why does PHP echo'd text lose it's formatting?

I cant get the new line to function work right. It just comes out as one line. Like this -

Make: Sony Model: a Processor: Intel Core 2 Duo

This is the code for it ---

$message="Make: " . $_POST['make'] . "\r\n Model: " . $_POST['model'] . "\r\n Processor: " . $_POST['processor'];

When this is sent as an email it works perfect but when i do

echo $message;

it just comes out as the above - it all on one line. How can i make this work?

thankyou

回答1:

You are presumably echoing this onto a web page.

Browsers do not (or at least, should not) respect literal new lines, you have to use the HTML <br> tag instead.

Try this:

echo str_replace(array("\r\n","\r","\n"),'<br>',$message);
// or
echo nl2br($message);


回答2:

HTML ignores newlines.

Instead, you should use <br />, <pre>, or <table>.



回答3:

If you want print it using HTML you'll either need to use <br /> or use the nl2br() function.

Using <br />:

$message = 'Make: ' . $_POST['name'] . '<br />'; //...

If you want to send plaintext, set an appropriate HTTP header, e.g.

header('Content-Type: text/plain');

Most browsers will take this as a hint to display it in 'raw mode'.



标签: php newline