swapper3d Posted March 9, 2010 Share Posted March 9, 2010 Ok, here's the deal. I'd like to save a text from a html text field into a MySQL database. Of course the text needs to be word wrapped, with <br /> for html view and all line breakers also needs to be replaced with <br />. Next step is to be able to edit the text saved earlier in the database from within another html text field and save it back into (update) the database. Problem is, I haven't been able to find a good way to "unwrap" the text (replace the <br />'s). I've tried different combinations of wordwrap(), nl2br() and str_replace(), but with no success. I've also been searching on the internet for a solution, but can't seem to find any info on this even though it seems to me as a not too unusual function. I did actually find something on php.net's wordwrap function page but it does not seem to help me get any closer to my goal. I just keep ending up with regular blank spaces instead of the <br>'s... $text = preg_replace('/[\t\r\n]+/', ' ', $text); So, what would be the best and easiest way of doing this? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/194679-text-unwrap/ Share on other sites More sharing options...
mapleleaf Posted March 9, 2010 Share Posted March 9, 2010 textarea or just a regular input text field? If it is a textarea use tinymce or FckEditor. Otherwise you will need to decide when to cut the string which might not work depending on how you are using your form Quote Link to comment https://forums.phpfreaks.com/topic/194679-text-unwrap/#findComment-1023838 Share on other sites More sharing options...
nafetski Posted March 9, 2010 Share Posted March 9, 2010 Alright, so what you want to do is #1 - convert your newlines to <br /> when inserting into the database. (that way when you echo the HTML view out, they show up the way you want). Then #2 - When you go to say...edit that same record, you want the <br /> to turn back into \n (newlines) This function might help you with that (should preserve the newlines for textareas, and not show \n). (Tho the poster above me is right, for what you want to do you would probably LOVE tinyMCE...great app) <?php function br2nl($string){ $return=eregi_replace('<br[[:space:]]*/?'. '[[:space:]]*>',chr(13).chr(10),$string); return $return; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/194679-text-unwrap/#findComment-1023885 Share on other sites More sharing options...
swapper3d Posted March 15, 2010 Author Share Posted March 15, 2010 Thank you for your replies! mapleleaf, looked into FckEditor a little but it doesn’t seem like what I’m looking for here, mainly because the site I’m building is suppose to be lightweight and kept very simple. nafetski, I've been playing around with your function during the weekend, and I actually think I almost have straitened this out by now. Though I’m sorry I can’t say that it helped me very much without some modifications. eregi_replace('<br[[:space:]]*/?' . '[[:space:]]*>',chr(13).chr(10),$string); Can you, please, for the record, explain exactly more in detail what this part does? However I found out that my main problem was that I had to differ a \n from a "wordwrap" when stored in the database. I did this by formatting the text the following way before inserting it into the database: $wordwrap = nl2br(wordwrap($_POST["directive"], "75", " <br>")); //Note that nl2br() inserts '<br />' and (here) wordwrap inserts just '<br>'. //Also note the blank space before '<br>', which is crucial in this case. And when I echo it back in the form text field for editing I used: $nowordwrap = eregi_replace("<br>", "", $string); $br2nl = eregi_replace("<br />", "", $nowordwrap); I’m happy because it works! But, I have to admit that it does not seem like a possible solution if you look at the code… Probably nl2br() returns more than just “<br />”, maybe more like “<br />\n”? Only problem with this is that wordwrap() does not wordwrap on very suitable places.. Anyone has a solution to this? However, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/194679-text-unwrap/#findComment-1026670 Share on other sites More sharing options...
wildteen88 Posted March 15, 2010 Share Posted March 15, 2010 You should do this $wordwrap = nl2br(wordwrap($some_var, "75", " <br>")); //Note that nl2br() inserts '<br />' and (here) wordwrap inserts just '<br>'. //Also note the blank space before '<br>', which is crucial in this case. When you get the data out of your database, not when you insert it. All newlines are saved in the database, it just web browsers ignore newlines (whitespace) characters this is why you have to use <br /> to display a new line. You'' save yourself a lot of trouble doing it this way. Quote Link to comment https://forums.phpfreaks.com/topic/194679-text-unwrap/#findComment-1026674 Share on other sites More sharing options...
Psycho Posted March 15, 2010 Share Posted March 15, 2010 You should do this $wordwrap = nl2br(wordwrap($some_var, "75", " <br>")); //Note that nl2br() inserts '<br />' and (here) wordwrap inserts just '<br>'. //Also note the blank space before '<br>', which is crucial in this case. When you get the data out of your database, not when you insert it. All newlines are saved in the database, it just web browsers ignore newlines (whitespace) characters this is why you have to use <br /> to display a new line. You'' save yourself a lot of trouble doing it this way. ^^ - In total agreement. Store all input in it's "native" format. Only transform it for display purposes at run time: i.e. nl2br(), date(), number_format(), etc. Quote Link to comment https://forums.phpfreaks.com/topic/194679-text-unwrap/#findComment-1026677 Share on other sites More sharing options...
swapper3d Posted March 15, 2010 Author Share Posted March 15, 2010 You should do this $wordwrap = nl2br(wordwrap($some_var, "75", " <br>")); //Note that nl2br() inserts '<br />' and (here) wordwrap inserts just '<br>'. //Also note the blank space before '<br>', which is crucial in this case. When you get the data out of your database, not when you insert it. All newlines are saved in the database, it just web browsers ignore newlines (whitespace) characters this is why you have to use <br /> to display a new line. You'' save yourself a lot of trouble doing it this way. ^^ - In total agreement. Store all input in it's "native" format. Only transform it for display purposes at run time: i.e. nl2br(), date(), number_format(), etc. Ahh, of course! I'll remember that. Sort of been inside this little box when it came to that... Quote Link to comment https://forums.phpfreaks.com/topic/194679-text-unwrap/#findComment-1026679 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.