tags:

views:

483

answers:

1

Any one know how to convert string value into html format in php? For Example:

$string = "Hello World!!
           How are you?"

If I echo $string, it will display like this:

Hello World!!How are you?

Instead of:

Hello World!!
How are you?

Any way php can conver the $string into html format? If I input:

$string = "Hello World!!
           How are you?"

Php will convert it to become:

$string = "Hello World!!<br>How are you?"
+6  A: 

You’re looking for the nl2br function that adds an HTML line break tag <br /> to every physical line break sequence (\n, \r or \r\n):

\n    →  <br />\n
\r    →  <br />\r
\r\n  →  <br />\r\n
Gumbo
Keep in mind this will only convert the carriage returns (enter characters) into <br /> tags.
Mladen Mihajlovic
@Mladen Mihajlovic: No it doesn’t. It adds a HTML line break to every physical line break sequence: `\r\n`, `\n` and `\r` → `<br />\r\n`, `<br />\n` and `<br />\r`
Gumbo