Jump to content

nl2br small problem


Recommended Posts

I got this question in my mind.....when should we use nl2br?

is it used during inserting the data??

 

like $data=nl2br($_POST['text']);

So then i will have all html code stored inside the database like <br><br>

 

but what if let say i type "my name is <br> John" in the textbox

 

will this <br>changed into newline when retrieving the data from database??

Link to comment
Share on other sites

I got this question in my mind.....when should we use nl2br?

is it used during inserting the data??

 

like $data=nl2br($_POST['text']);

So then i will have all html code stored inside the database like <br><br>

 

but what if let say i type "my name is <br> John" in the textbox

 

will this <br>changed into newline when retrieving the data from database??

 

Yes it would become a linebreak literally.

 

Unless you escape it (with htmlentities) or strip it etc.

 

Link to comment
Share on other sites

The nl2br() function should only be used when displaying text that might contain the newline character on the screen. The newline character is NOT the html tag "<br \>", but the character(s) represented by "\n", "\r", or "\r\n" depending on the OS your script runs on.

 

The function doesn't replace the newline with "<br \>" but adds the "<br \> before the newline in the string.

 

Ken

Link to comment
Share on other sites

Yes it would become a linebreak literally.

 

No it wouldn't. it would be untouched.

 

Anyway, to the op. Its generally best practice to store your data in the format it was received in (once being validated of course). You can then format the output in a choice of different ways depending on the application.

Link to comment
Share on other sites

Yes it would become a linebreak literally.

 

No it wouldn't. it would be untouched.

 

Mind to explain?

 

<?php
echo nl2br("Bob <br /> Bob2 <br />");
?>

// Source 
Bob <br /> Bob2 <br />

 

 

A <br /> tag is NOT a line break.

Link to comment
Share on other sites

Yes it would become a linebreak literally.

 

No it wouldn't. it would be untouched.

 

Mind to explain?

 

<?php
echo nl2br("Bob <br /> Bob2 <br />");
?>

// Source 
Bob <br /> Bob2 <br />

 

 

A <br /> tag is NOT a line break.

 

Ok its an HTML line break? Thats what he was asking if you ask me..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.