tags:

views:

52

answers:

1

I am taking a string from textarea and exploding it and trimming each line of the array with array_map():

$answers = explode("\n", $data['answers']);
// remove all whitespace such as \r (carriage return)
$asnwers = array_map('trim', $answers);

Then I store each array value in a separate row in a table answers in the database. The problem is there seems to be \n character at the end of each answer in the database. When I echo answers in HTML like this:

<?php foreach ($this->answers as $a): ?>
                <tr>
                    <td><?php echo $this->escape($a->body); ?></td>
                </tr>
<?php endforreach; ?>

When I then look at the HTML source I see this:

                <tr>
                    <td>Some random answer
</td>
                </tr>

As you can see, there is a newline (probably \n) at the end of the string because the closing tag gets moved to the next line.

What I am doing wrong?

+6  A: 
$asnwers = array_map('trim', $answers);

You're assigning the return value of array_map to $asnwers. It should be $answers.

Johannes Gorset
+1 That's also what i was thinking (although it could also just be a simple typo in the question)
ChristopheD
Damn, how could I not see that.
Richard Knop