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
https://forums.phpfreaks.com/topic/201004-nl2br-small-problem/
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
https://forums.phpfreaks.com/topic/201004-nl2br-small-problem/#findComment-1054586
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
https://forums.phpfreaks.com/topic/201004-nl2br-small-problem/#findComment-1054591
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
https://forums.phpfreaks.com/topic/201004-nl2br-small-problem/#findComment-1054592
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
https://forums.phpfreaks.com/topic/201004-nl2br-small-problem/#findComment-1055493
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.