Yash Mathur
Yash Mathur

Reputation: 69

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

Upvotes: 1

Views: 554

Answers (5)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385108

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.

Upvotes: 0

Sam
Sam

Reputation: 71

\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;

Upvotes: 0

Gowri
Gowri

Reputation: 16835

using preg_replace

simply replace it

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

Upvotes: 0

Emil Vikstr&#246;m
Emil Vikstr&#246;m

Reputation: 91902

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.

Upvotes: 1

genesis
genesis

Reputation: 50966

nl2br($string);

is fast and easy

Upvotes: 6

Related Questions