Hi all,

I am looking to understand Html entities & html special chars in more details.

I have a form and text area that users complete, the databse updates correctly and displays the information.

The problem im facing is this:
When the database displays the data, its display incorrectly.

For example:
If the user enters
Line 1
Line 2
Line 3

The data is stored correctly in the database, including the line brakes.
But when the data is displayed on the page, it is displaying as
Line 1 Line 2 Line 3 ? How can I display the data from the database correctly.

Here is my form

<form style="width:100%;" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<div><div class="p">
    <textarea name="sabout" cols="35" rows="5" class="ta" id="sabout" style='width:99.9%;'/><?php echo htmlspecialchars($_POST['sabout']); ?></textarea></form>

I have the following variable to display the data from the database -

$sabout = htmlspecialchars($sabout);

Displaying the variable in my page

<?php if($sabout){echo $sabout; }?>

any help would be appreciated, thanks

Dani AI

Generated

Nice work by —nl2br is the right tool to turn stored newlines into visible breaks in HTML. A couple of clarifications and safe practices will make the behavior reliable and secure across your app.

Always escape user text first, then convert newlines. Escaping (htmlspecialchars) prevents XSS; nl2br after that inserts real <br> tags that the browser will render. For example:

$clean = htmlspecialchars($numrows['description'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
echo nl2br($clean);

When re-populating a textarea (the form field), do not use nl2br—keep raw newlines there so the user sees their original line breaks. Also make sure your <textarea> is not self-closing; it must be <textarea>content</textarea>. Example for a textarea value:

<textarea name="sabout"><?php echo htmlspecialchars($sabout, ENT_QUOTES, 'UTF-8'); ?></textarea>

A few extra tips tied back to the thread: store raw user input in the database and escape on output; avoid calling htmlspecialchars after nl2br (that would neutralize the breaks); normalize line endings if you see mixed CR/LF problems; and use prepared statements for DB writes. If you need paragraph blocks instead of single <br> breaks, split on double-newlines and wrap chunks in <p> tags rather than relying only on nl2br.

Recommended Answers

All 6 Replies

$sabout=nl2br($sabout);

Give that a shot. When data is entered into a DB hard returns (hitting the Enter button) will be converted to \n (newline) \r (return) or a combination of both. nl2br will take newlines and returns and convert them to the html linebreak &lt;br /&gt;

commented: Useful post +7

- You surely do rock, that works a treat, thank you :)

just one more question if you dont mind. . . I am trying to add the above into a different part of my code -

the variables are

".$numrows[description]."

How can I add your code to the above ?

Member Avatar for Member #120589
".nl2br($numrows['description'])."

sorry for jumping in

@ardav - not a problem about jumping in, cheers for the input, its works a treat :)

Mark this thread as solved if your problem solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.