简体   繁体   中英

How do i enable line breaks in php?

I am making a comments system in which i can accept user input with line breaks. I don't want to show the \n or \r thing Please help me with this

nl2br($string);

is fast and easy

They are enabled by default. If you are outputting the text to a web browser, make sure to use nl2br or the white-space attribute in CSS.

\n should do the trick.

if you are trying to output a textarea, then use nl2br();

also:-

If you are trying to format your HTML source, you should use the constant PHP_EOL. The main reason being that on windows machines the EOL is \r\n and on UNIX machines it is \n. With a properly installed LAMP set up just use PHP_EOL like so.

$html.="<p>This is my HTML</p>" . PHP_EOL;

using preg_replace

simply replace it

preg_replace('/\n/'," ",$str);

Line breaks will be stored just like any other character.

\n is an escape code that allows you to explicitly insert a line break into a string, but I don't think that it's relevant here.

The issue you're actually facing is that HTML does not impart any visual meaning to a line break. Line breaks within HTML code do not , under normal circumstances, equate to a line break on the screen.

One way to render a line break in HTML is to use a line break tag, or <br> .

In PHP, you can automatically convert line breaks to <br> with the nl2br function. Applying this to your comment text when you output it into HTML will enable you and other visitors to see the line break visually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM