Jump to content

[SOLVED] PHP replace newline from Textarea


FlyingIsFun1217

Recommended Posts

Hey!

 

Sorry, I think this is more of an HTML problem, but I think I'll get more help here.

 

I've got a textarea, in which users will most likely have newlines. Since I'm saving this to a database, I want to replace all of the newline characters (is a textarea's newline '\n') with '<br>'. I imagine I'll end up using 'eregi_replace', I just need to know what the newline character of a textarea is.

 

Thanks!

FlyingIsFun1217

Link to comment
Share on other sites

Ok, I know what you're trying to do. It's better to just insert the data as is(text filtered of course) and then when the data is being viewed you just do a quick $var = str_replace("\n", "<br>", $original) and then echo $var. This is your best bet bc if the user is allowed to edit that text later on, they will be editing the text with <br> tags unless you reverse the tags before showing the text again to edit($var = str_replace("<br>", "\n", $original)) but save yourself from having to do that by doing what i said before. Insert text unchanged in the database and then convert the newline characters "\n" into <br>'s just before echo-ing it.

 

Hope that helped

Link to comment
Share on other sites

Actually nl2br() will work fine for output. That is unless you're specifically trying to get rid of all newline characters; period. If that's the case use something like the code below to get rid of all newline and character returns.

<?php
preg_replace("/[\n\r]/","", $some_value);
?>

 

Link to comment
Share on other sites

I'm mostly just trying to replace whatever is saved as the newline character from a textarea as a break tage (for HTML), so that the following:

 

Line one to

line two.

 

Turns into:

 

Line one to<br>line two.

 

I'll try the tip provided by gijew, looks somewhat like what I would need :)

 

Thanks!

FlyingIsFun1217

Link to comment
Share on other sites

And yes, that seemed to do the trick! But the problem...

 

It inserts two break tags since it's finding (I'm assuming, since I saw something saying windows, linux, and mac return different newline characters in textarea) '\r\n' for a newline. Is there any quick way to replace either '\r\n', '\r', or '\n', and only worry about those?

 

Thanks!

FlyingIsFun1217

Link to comment
Share on other sites

I totally agree with blueman. Using nl2br() really IS the best way to output but you're hell-bent on removing those suckers so I'm here to help you try :)

 

Not the most elegant solution but:

$string = preg_replace("/[\n\r]/","<br />", $some_value);
$string = str_replace('<br /><br />','<br />', $string);

Link to comment
Share on other sites

like some others have said jsut run it through nl2br() this does that for you, but if the user tries to edit this text later it will show up in the text area exactly as the user sent it, new lines and all,

 

That seems to do it!

 

Ahh, so simple... Thanks again!

FlyingIsFun1217

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.