Cyclone
Cyclone

Reputation: 18295

Replacing a new line with its html equivalent in PHP

I am facing a bit of a quandary, I need to replace a new line with <br />. Now, clearly, replacing all instances of \n did not work, as the page did not have proper linebreaks. Here is an example of some possible text:

Some text


More text

Now, this is an issue because there is no \n and I have no way to auto-insert <br />. How can I ensure that this contains proper linebreaks?

This is in PHP. I cannot serve it as plain text.

Upvotes: 0

Views: 1158

Answers (4)

Jeff
Jeff

Reputation: 742

php has a built in function for that.

nl2br() i believe.

Upvotes: 2

Jordan S. Jones
Jordan S. Jones

Reputation: 13903

You will want to use nl2br.

Your text has to have a newline (\n) or carriage return (\r) if the text is on 2 different lines.

nl2br will handle either case.

Upvotes: 3

FrEaKmAn
FrEaKmAn

Reputation: 1845

To replace new line breaks with
just use nl2br

Upvotes: 10

Vinko Vrsalovic
Vinko Vrsalovic

Reputation: 340476

Maybe you want http://php.net/nl2br? Or maybe I have misunderstood...

Upvotes: 6

Related Questions