physaux Posted December 17, 2009 Share Posted December 17, 2009 Hey guys, so I wrote a quick little script to help me with some text manipulation that I have to do. Here is the basic gist of it: I get a header, body, and footer text. I copy- paste these strings from a TEXT FILE to a FORM, which submits these values as POST. I hit "submit", and my script "joins them together", and replaces some names (and more string manipulation). It then outputs the result on the next page. So everything works, my string is manipulated like I want it to be, but... My problem is that when I copy- paste a 3 paragraph for the body, when my script outputs it, it is all compressed into 1 large paragraph. Would anyone know how I could "replace" the new line character with a "<br>" somehow, so that in the outputted html my body retains the same structure? I hope this makes sense to you guys, if not please ask for clarification. Thank you!! Quote Link to comment https://forums.phpfreaks.com/topic/185504-string-manipulation-how-to-preserve-new-line-complicated-help-plz/ Share on other sites More sharing options...
JAY6390 Posted December 17, 2009 Share Posted December 17, 2009 $output = nl2br($input); Quote Link to comment https://forums.phpfreaks.com/topic/185504-string-manipulation-how-to-preserve-new-line-complicated-help-plz/#findComment-979391 Share on other sites More sharing options...
physaux Posted December 17, 2009 Author Share Posted December 17, 2009 nl2br doesn't seem to be working.. Any Idea why? Here is my code: if(isset($_POST['original'])) $original = nl2br($_POST['original']); echo $original; <form name="input" action="maketext.php" method="post"> <table border="1" cellspacing="1" cellpadding="1"> <tr> <td>Original:</td> <td><input type="text" name="original" /></td> </tr> <tr> //.... when I copy- paste a two paragraph text, it comes out fused to 1 paragraph! Quote Link to comment https://forums.phpfreaks.com/topic/185504-string-manipulation-how-to-preserve-new-line-complicated-help-plz/#findComment-979441 Share on other sites More sharing options...
mikesta707 Posted December 17, 2009 Share Posted December 17, 2009 where do you copy it from? Word or something? I'm guessing that the text itself doesn't have new line characters, and thats why the nl2br() function won't work. You can insert <br /> tags or better yet, <p> tags into the textarea manually, but I don't see a way for php to guess where your paragraphs ends/begins and insert a tag there Quote Link to comment https://forums.phpfreaks.com/topic/185504-string-manipulation-how-to-preserve-new-line-complicated-help-plz/#findComment-979450 Share on other sites More sharing options...
physaux Posted December 17, 2009 Author Share Posted December 17, 2009 Well, I am actually using the script to manipulate wordpress posts, so that is where I am copying the text from. However, I tested the script on sample text from a text file in (text edit in os X), and I had the same problems. EDIT: -I also tested it on random paragraphs copied from a web page, I get the same problem again!! It never works :'( Quote Link to comment https://forums.phpfreaks.com/topic/185504-string-manipulation-how-to-preserve-new-line-complicated-help-plz/#findComment-979465 Share on other sites More sharing options...
JAY6390 Posted December 17, 2009 Share Posted December 17, 2009 If this is with wordpress, then you are better off using a plugin and just outputting the text differently using that (use nl2br on the text) Quote Link to comment https://forums.phpfreaks.com/topic/185504-string-manipulation-how-to-preserve-new-line-complicated-help-plz/#findComment-979492 Share on other sites More sharing options...
physaux Posted December 17, 2009 Author Share Posted December 17, 2009 I think I realized my problem, but I do not know how to fix it. Here is the deal- When I try to type a value manually, without copy and paste, I cannot add a second paragraph by pressing "enter/return". When I finish writing my first paragraph and hit "enter/return" to start my second, it automatically submits only my first paragraph. This leads me to believe that my form is not set up properly, to even "take in" "enter/return" new lines. Does anyone know how I could fix this? Here is what I have now: <form name="input" action="maketext.php" method="post"> <table border="1" cellspacing="1" cellpadding="1"> <tr> <td>Original:</td> <td><input type="text" name="original" /></td> </tr> <tr> //.... perhaps the input type should be textarea? idk much about this stuff, I really appreciate everyone's help! what do you think?? Quote Link to comment https://forums.phpfreaks.com/topic/185504-string-manipulation-how-to-preserve-new-line-complicated-help-plz/#findComment-979499 Share on other sites More sharing options...
mikesta707 Posted December 17, 2009 Share Posted December 17, 2009 oh... you don't have a text area... yeah thats the problem, make it a text area. normal text input boxes don't have new lines (because all the text is on one line) Quote Link to comment https://forums.phpfreaks.com/topic/185504-string-manipulation-how-to-preserve-new-line-complicated-help-plz/#findComment-979502 Share on other sites More sharing options...
JAY6390 Posted December 17, 2009 Share Posted December 17, 2009 Ah, yeah that is the issue Quote Link to comment https://forums.phpfreaks.com/topic/185504-string-manipulation-how-to-preserve-new-line-complicated-help-plz/#findComment-979507 Share on other sites More sharing options...
physaux Posted December 17, 2009 Author Share Posted December 17, 2009 Ok, could somebody clear up how exactly I would make the input be a textarea? I have never used it before... Here is what I am trying and it is not working: <form name="input" action="maketext.php" method="post"> <table border="1" cellspacing="1" cellpadding="1"> <tr> <td>Original:</td> <td><input type="textarea" name="original" /></td> </tr> <tr> <td>Author Name:</td> <td><input type="text" name="author" /></td> </tr> <tr> //... How do I do that, so that it's "name" is "original", so that it will be submited with the form, and can be accessed at the next page from $_POST['original']. Thank you!! Quote Link to comment https://forums.phpfreaks.com/topic/185504-string-manipulation-how-to-preserve-new-line-complicated-help-plz/#findComment-979511 Share on other sites More sharing options...
Philip Posted December 17, 2009 Share Posted December 17, 2009 <textarea name"original"></textarea> Quote Link to comment https://forums.phpfreaks.com/topic/185504-string-manipulation-how-to-preserve-new-line-complicated-help-plz/#findComment-979513 Share on other sites More sharing options...
JAY6390 Posted December 17, 2009 Share Posted December 17, 2009 <textarea name="original" id="original"></textarea> http://www.w3schools.com/TAGS/tag_textarea.asp Quote Link to comment https://forums.phpfreaks.com/topic/185504-string-manipulation-how-to-preserve-new-line-complicated-help-plz/#findComment-979514 Share on other sites More sharing options...
physaux Posted December 17, 2009 Author Share Posted December 17, 2009 Great, it is working now!! Thanks so much for all your help!! Quote Link to comment https://forums.phpfreaks.com/topic/185504-string-manipulation-how-to-preserve-new-line-complicated-help-plz/#findComment-979516 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.