Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP New Line Not Working [duplicate]

Tags:

php

newline

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

like image 766
Dr.Pepper Avatar asked Oct 28 '11 13:10

Dr.Pepper


3 Answers

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);
like image 193
DaveRandom Avatar answered Sep 28 '22 18:09

DaveRandom


HTML ignores newlines.

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

like image 43
SLaks Avatar answered Sep 28 '22 19:09

SLaks


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'.

like image 37
middus Avatar answered Sep 28 '22 18:09

middus