PHP new line not working

Possible duplicate:
Why does PHP text echo'd lose formatting?

I can not get the new line to work correctly. It just exits 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 it is sent as an email, it works fine, but when I do

 echo $message; 

it just comes out as above - it's all on the same line. How can I do this job?

Thankyou

+6
source share
3 answers

Supposedly you echo this on a web page.

Browsers cannot (or at least shouldn't) respect literal newlines; instead, you should use the HTML <br> tag.

Try the following:

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

HTML ignores newline characters.

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

+3
source

If you want to print it using HTML, you will need to use <br /> or use nl2br() .

Using <br /> :

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

If you want to send plaintext, set the appropriate HTTP header, for example

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

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

+2
source

Source: https://habr.com/ru/post/900290/


All Articles