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

2 Answers 2

8

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
Sign up to request clarification or add additional context in comments.

2 Comments

Keep in mind this will only convert the carriage returns (enter characters) into <br /> tags.
@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
1
$string = "Hello World!!
       How are you?";
echo nl2br($string);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.