Jin Yong
Jin Yong

Reputation: 43788

Convert string value into html format in php

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?" 

Upvotes: 1

Views: 5982

Answers (2)

sas
sas

Reputation: 2597

$string = "Hello World!!
       How are you?";
echo nl2br($string);

Upvotes: 1

Gumbo
Gumbo

Reputation: 655845

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

Upvotes: 8

Related Questions