Alright so for my site I am allowing my users to have a description of themselves or whatever they like, however when I attempt to make breaks using [ENTERKEY] into the <textarea>
it looks like this:
Hello, I am John Smith.
Phone#: (123)456-7890
I enjoy web-browsing.
When I return to the page it looks EXACTLY the same (It puts their current description in the edit box). This is what I want. I look in the PHP database and it still looks the same. Again it is what I want. However on the profile page It looks like this
Hello, I am John Smith. Phone#: (123)456-7890 I enjoy web-browsing.
It is contained inside a div with these style tags and like so
<div style="width: 250px; min-height: 50px; margin: auto; font-weight: normal; text-align: center; padding: 2px; margin-bottom: 5px;">
<?php echo $description; ?>
</div>
Im curious why it does this any help would be appreciated :D.
Add white-space:pre-line
to your <div>
Or, use:
<?= nl2br($description); ?>
Remember that HTML needs <br />
for line breaks, not or
(like your
<textarea>
is collecting). So you can either tell HTML to pay attention to those new lines using white-space
, or force the <br />
using nl2br
.
When you enter a newline in a textarea, it gets stored as , however, HTML does not honor
linebreaks, which is why everything shows up in a single line when inside a div.
To fix this, you have to convert the to
<br />
(using nl2br()
)which HTML recognizes:
<div style="width: 250px; min-height: 50px; margin: auto; font-weight: normal; text-align: center; padding: 2px; margin-bottom: 5px;">
<?php echo nl2br($description); ?>
</div>
This is because when you take a regular line break and try to render it as HTML, HTML ignore the whitespace character. You need to explictily use <br>
(or <br/>
depending on DOCTYPE) to create a line break in HTML.
The easiest way to do this in PHP is by using nl2br()
function in PHP on output.