如何在php中启用换行符?

如何在php中启用换行符?

问题描述:

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

我正在建立一个评论系统,我可以接受用户输入和 strong>换行符。 我不想显示 code>或 code>的东西 请帮我解决这个问题 p> div>

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.

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 and on UNIX machines it is . 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('/
/'," ",$str);

Line breaks will be stored just like any other character.

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.