FlyingIsFun1217 Posted June 23, 2008 Share Posted June 23, 2008 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 Quote Link to comment Share on other sites More sharing options...
ober Posted June 23, 2008 Share Posted June 23, 2008 I'm not clear on what you want to replace it with or why? Are you sure you're not after the nl2br() function? Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted June 23, 2008 Share Posted June 23, 2008 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 Quote Link to comment Share on other sites More sharing options...
gijew Posted June 23, 2008 Share Posted June 23, 2008 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); ?> Quote Link to comment Share on other sites More sharing options...
FlyingIsFun1217 Posted June 23, 2008 Author Share Posted June 23, 2008 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 Quote Link to comment Share on other sites More sharing options...
gijew Posted June 23, 2008 Share Posted June 23, 2008 preg_replace("/[\n\r]/","<br />", $some_value); Good luck Quote Link to comment Share on other sites More sharing options...
FlyingIsFun1217 Posted June 23, 2008 Author Share Posted June 23, 2008 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 Quote Link to comment Share on other sites More sharing options...
blueman378 Posted June 23, 2008 Share Posted June 23, 2008 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, Quote Link to comment Share on other sites More sharing options...
gijew Posted June 23, 2008 Share Posted June 23, 2008 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); Quote Link to comment Share on other sites More sharing options...
FlyingIsFun1217 Posted June 23, 2008 Author Share Posted June 23, 2008 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 Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted June 24, 2008 Share Posted June 24, 2008 So is my way just wrong? Is there a reason my advice was ignored? Quote Link to comment Share on other sites More sharing options...
gijew Posted June 24, 2008 Share Posted June 24, 2008 I wasn't ignoring you danny Actually he ignored the nl2br() suggestion like 4 times before he got it Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted June 24, 2008 Share Posted June 24, 2008 just wondering id nl2br is functionally better than str_replace. Often times there are many ways to do the same thing, but one is more efficient. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.