Rick Salad
Rick Salad

Reputation: 83

delete the <br /> from the textarea?

I am trying to do a line break after the message "Original message", I have tried with this but It keeps showing me the

---Original message---<br />
message

<textarea id="txtMessage" rows="10" cols="50"><?php echo nl2br(str_replace('<br/>', " ","---Original message---\n".$array['message']));?></textarea>

I want something likes this:

---Original message---
message

any advise?

Upvotes: 0

Views: 5399

Answers (6)

Jade
Jade

Reputation: 649

If you want to do nl2br on all of the text except for what's inside the textarea you could do this:

function clean_textarea_of_br($data) {
     return str_replace(array("<br>", "<br/>", "<br />"), "", $data[0]);
}

$message = preg_replace_callback('#<textarea[^>]*>(.*?)</textarea>#is',clean_textarea_of_br,$message);

Upvotes: 0

Bojan Kogoj
Bojan Kogoj

Reputation: 5649

This should do what you want it to:

<?php echo str_replace('<br />', " ","---Original message---\n".$array['message']);?>

nl2br — Inserts HTML line breaks before all newlines in a string (from php.net)

Example:

echo "<textarea>HI! \nThis is some String, \nit works fine</textarea>";

Result:

new line example in textarea

But if you try this:

echo nl2br("<textarea>HI! \nThis is some String, \nit works fine</textarea>");

you will get this:

new line with nl2br in texarea

Therefore you should not use nl2br before saving it to database, otherwise you have to get rid of <br /> every time you try to edit text! Just use it when you print it out as text.

Upvotes: 4

bfavaretto
bfavaretto

Reputation: 71908

You are removing the HTML breaks, then adding them back! Look at your code:

nl2br(str_replace('<br/>', " ","---Original message---\n".$array['message']))

First, str_replace replaces '<br/>' with a space. Then, nl2br adds a <br> for every newline (\n) it finds.

Remove nl2br call and it's done.

Upvotes: 0

Marc B
Marc B

Reputation: 360602

You're trying to replace <br/>, but the original text has <br /> (note the space).

Upvotes: 0

Elliot Nelson
Elliot Nelson

Reputation: 11557

The php function "nl2br" takes newlines, and converts them into br tags. If you don't want that, you should probably remove it :).

Heh, beaten by Ryan.

Upvotes: 1

SeanCannon
SeanCannon

Reputation: 77966

echo nl2br(str_replace('<br/>', " ", ... ));

should be

echo str_replace('<br />', ' ', ... );

Upvotes: 1

Related Questions