开发者

Trim doesn't remove \n

开发者 https://www.devze.com 2022-12-20 17:30 出处:网络
I am taking a string from textarea and exploding it and trimming each line of the array with array_map():

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.

Wh开发者_如何学运维at I am doing wrong?


$asnwers = array_map('trim', $answers);

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

0

精彩评论

暂无评论...
验证码 换一张
取 消