textarea不提供html标签

This may be a very simple and stupid question, but it's so simple that I don't know what I did wrong.

I'm trying to get the content of a textarea while retaining the formatting (breakline, spaces, etc). but when i tried to post the form, there was no html tags to be found. all I got was plain text.

when I type in the textarea like this:

first line



after 3 br

I expected the result of echo $foo; to be something like this:

first line
<br/>gt;
<br/>gt;
<br/>gt;
after 3 br

but instead, the result of echo $foo; was still like this:

first line



after 3 br

this is the display.php:

<form method="post" action="">
  <textarea name="foo"><?php echo set_value('foo'); ?></textarea>
  <br/>
  <input type="submit" name="submit" value="Submit"/>
</form>

this is the controller:

function form_foo()
{
  $foo = $this->input->post('foo');
  if (get_magic_quotes_gpc())
  {
    $foo = stripslashes($foo);
  }
  $foo = htmlentities($foo);
  echo $foo;
  $this->load->view('display', $this->data);
}

what did I do wrong?

P.S : I'm using codeigniter 3.0 and PHP 5.6.15

Try this code -

function form_foo()
{
    $foo = $this->input->post('foo');
    if (get_magic_quotes_gpc())
    {
        $foo = stripslashes($foo);
    }

    //$foo = htmlentities($foo);

    echo $foo;
    $this->load->view('display', $this->data);
}

Why don't you use contenteditable?

<div contenteditable="true"></div>

This can turn <div> into editable area but without style. You need to style them additionally.

Here is my CSS solution

[contentEditable=true] {
    white-space: pre;
    background-color: #FFFFFF;
    min-height: 6em;
    max-height: 12em;
    overflow-y: scroll;
    outline: none !important;
    border-radius: 4px;
    padding: 4px;
}

Beware of dealing with the HTML tags.

See reference contenteditable.