user962449
user962449

Reputation: 3873

remove \n from paragraph

I have a jquery editable div that when you click on it you can edit the text. The problem is that when the data is called from the db and placed into the paragraph I keep getting a \n for every space. How can I replace the \n with an actual new line.

I tried nl2br(), but that's not very convenient for my users since they then have to play with the <br /> when they want to edit the paragraph.

Any thoughts?

Upvotes: 0

Views: 372

Answers (5)

vascowhite
vascowhite

Reputation: 18440

try:-

$strippedText = str_replace(chr(10), '', $textFromDB);

or

$strippedText = str_replace(chr(10), '<br/>', $textFromDB);

Does this work? (Working on the possiblity that the newlines are already escaped).

$strippedText = str_replace('\\n', ' ', $textFromDB);

Upvotes: 1

Syntax Error
Syntax Error

Reputation: 4527

Ok, I think this is different enough that I should do a separate answer for it.

Are you saying a literal "slash n" shows up on the page? Or are you saying that your newlines show up as spaces?

If it's the latter, then there's no way around that. HTML will show newlines only as a space - you have to convert to br tags to break the line if it's not in a textarea context. But you can always convert them back to newlines when you pop that textarea up for your user and this should work well for people.

Upvotes: 0

Bryan
Bryan

Reputation: 2211

What about:

str_replace("\\n", "", $str); // see if this gets rid of them

Then this should work to put actual newlines in there:

str_replace("\\n", "\n", $str); // should replace with actual newline

Upvotes: 1

pelelive
pelelive

Reputation: 637

Have you tried str_replace?

$myTextFromDB = str_replace('\n', PHP_EOL, $myTextFromDB);

Upvotes: 0

Syntax Error
Syntax Error

Reputation: 4527

Are you using a ready made solution or making your own? I use http://aloha-editor.org/ for stuff like this and it's mostly problem free.

Upvotes: 0

Related Questions