judeddokoinu Posted December 21, 2006 Share Posted December 21, 2006 Suppose you have a form with a <textarea>. When you submit the form, the php will assign the value of whatever is stored in the textarea to a variable. How do you detect where the user had pressed the enter/return key at?I'd like to be able to str_replace() the newlines/carriage returns in the string with html <br />'s but can't seem to determine how to detect their presence.I currently am trying the following, and have tried all the variations of the first value \n, \n\r, \r, \r\n, but no luck in getting it detected and replaced.[code]$newpostbody = str_replace('\n', '<br />', $newpostbody);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31450-solved-detect-newline-in-form-input/ Share on other sites More sharing options...
c4onastick Posted December 21, 2006 Share Posted December 21, 2006 This is common in PHP. There's actually a special function for it! nl2br().http://us3.php.net/manual/en/function.nl2br.phpAll you have to do is feed the string from the textarea to it and it'll do the rest (only works for newlines '\n' though.)[code]$htmlized_text = nl2br($_POST['textarea']);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31450-solved-detect-newline-in-form-input/#findComment-145665 Share on other sites More sharing options...
judeddokoinu Posted December 21, 2006 Author Share Posted December 21, 2006 Ah, very interesting. I was getting desperate, and found that if I use double quotes instead of singles, it worked fine. I did have to add the \r in order to get it to save to the database correctly, though, but all is well.solution:[code]$newpostbody = str_replace("\r\n", "<br />", $newpostbody);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31450-solved-detect-newline-in-form-input/#findComment-145667 Share on other sites More sharing options...
judeddokoinu Posted December 21, 2006 Author Share Posted December 21, 2006 c4, your code is a lot shorter than mine - I might have to try it instead. I do hope it replaces the \r in front of my \n as well Quote Link to comment https://forums.phpfreaks.com/topic/31450-solved-detect-newline-in-form-input/#findComment-145668 Share on other sites More sharing options...
judeddokoinu Posted December 21, 2006 Author Share Posted December 21, 2006 Alas, it does not remove the \r as well.[code]$newpostbody = nl2br($newpostbody);[/code]I suppose I may have to stick with my longer str_replace.And the reason I'm running it on the string and not directly on the $_POST is because I have several other replacement filters like to change <tab> into several nbsp's and such. Quote Link to comment https://forums.phpfreaks.com/topic/31450-solved-detect-newline-in-form-input/#findComment-145670 Share on other sites More sharing options...
c4onastick Posted December 21, 2006 Share Posted December 21, 2006 I don't think it does this puts a '<br />' infront of \n in the text. If that's what your data looks like and nl2br doesn't work give this a shot:[code]$htmized_text = preg_replace('/(\r\n)/', '<br \>\1', $source);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31450-solved-detect-newline-in-form-input/#findComment-145672 Share on other sites More sharing options...
trq Posted December 21, 2006 Share Posted December 21, 2006 FYI nl2br() doesn't actually replace anything. It just inserts html linebreaks (< /br>) just before newlines in a string. Quote Link to comment https://forums.phpfreaks.com/topic/31450-solved-detect-newline-in-form-input/#findComment-145674 Share on other sites More sharing options...
judeddokoinu Posted December 21, 2006 Author Share Posted December 21, 2006 [quote author=c4onastick link=topic=119478.msg489419#msg489419 date=1166680707]If that's what your data looks like and nl2br doesn't work give this a shot:[code]$htmized_text = preg_replace('/(\r\n)/', '<br \>\1', $source);[/code][/quote]that preg_replace still added a line break in the saved data. The output on-screen looks fine, but i need it converted to a single line when I place it in the database. Quote Link to comment https://forums.phpfreaks.com/topic/31450-solved-detect-newline-in-form-input/#findComment-145679 Share on other sites More sharing options...
c4onastick Posted December 21, 2006 Share Posted December 21, 2006 Oh! Ok! In that case then:[code]$htmized_text = preg_replace('/(\r\n)/', '<br \>', $source);[/code]That wont have the \r\n in it. Quote Link to comment https://forums.phpfreaks.com/topic/31450-solved-detect-newline-in-form-input/#findComment-145685 Share on other sites More sharing options...
trq Posted December 21, 2006 Share Posted December 21, 2006 You really should avoid storing html in the database whenever possible. What if (in the future) you want to display the data in another client besides a browser? Quote Link to comment https://forums.phpfreaks.com/topic/31450-solved-detect-newline-in-form-input/#findComment-145688 Share on other sites More sharing options...
c4onastick Posted December 21, 2006 Share Posted December 21, 2006 [quote author=thorpe link=topic=119478.msg489435#msg489435 date=1166682271]You really should avoid storing html in the database whenever possible. What if (in the future) you want to display the data in another client besides a browser?[/quote]How would you change the text? Would you recommend writing something to translate the text into the requested display format? (I'm curious, I've got a couple databases full of limited html, never even though twice about it.) Quote Link to comment https://forums.phpfreaks.com/topic/31450-solved-detect-newline-in-form-input/#findComment-145691 Share on other sites More sharing options...
trq Posted December 21, 2006 Share Posted December 21, 2006 [quote]How would you change the text?[/quote]Change the text to what?I allways leave stored data in its raw format. Then when I want display it I format it accordingly. ie, I wouldn't use nl2br until Im displaying the data, not before I store it. Quote Link to comment https://forums.phpfreaks.com/topic/31450-solved-detect-newline-in-form-input/#findComment-145693 Share on other sites More sharing options...
c4onastick Posted December 21, 2006 Share Posted December 21, 2006 Oh. I see. Duly noted. Good point. Quote Link to comment https://forums.phpfreaks.com/topic/31450-solved-detect-newline-in-form-input/#findComment-145698 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.