NamemeNick Posted April 23, 2009 Share Posted April 23, 2009 User fills a form that contains a textarea and I need help figuring out how to format the input string to store it in mysql for later retrieval and use in javascript. Suppose the user enters: line1 line2 When the form is posted, I read the field with $input=htmlentities($_POST['field']); Later, this input is used as argument in a javascript function: <script type="text/javascript">myFunction('<?php echo $input; ?>');</script> The problem is that javascript throws an "unterminated string" error because at run time, the browser sees a new line feed in the middle of the string like: <script type=text/javascript"> myFunction('line1 line2'); </script> My question is: how can I manipulate the input string so that input is on one line like "line1<br>line2" instead of "line 1 line2" ? I have tried using str_replace to read the $_POST data: $input=str_replace("\n","<br>",htmlentities($_POST['field'])); However this doesn't work. At runtime, the javascript becomes <script type="text/javascript"> myFunction('line1 <br>line2'); </script> <br> was put in, but the newline feed/character is still there, so the "unterminated string" error still comes up. Suggestions please? Quote Link to comment Share on other sites More sharing options...
Bauer418 Posted April 23, 2009 Share Posted April 23, 2009 str_replace("\n", "", nl2br($input)); Converts new lines to <br /> and then gets rid of the newline character. Quote Link to comment Share on other sites More sharing options...
premiso Posted April 23, 2009 Share Posted April 23, 2009 str_replace("\n", "", nl2br($input)); Converts new lines to <br /> and then gets rid of the newline character. Just use nl2br the str_replace portion is not needed, and does not make any sense, honestly. $input=htmlentities(nl2br($_POST['field'])); Not sure if you want it before the entities function or after, but that is the gist. Quote Link to comment Share on other sites More sharing options...
NamemeNick Posted April 23, 2009 Author Share Posted April 23, 2009 Thank you guys for the suggestions. I've tried both of them with no luck. Using nl2br alone places <br/> in front of the newline feed, but the newline feed is still present. <?php $input= nl2br(htmlentities($_POST['field']));?> Later, in javascript: <script type="text/javascript"> myFunction('<?php echo $input ?>'); </script> At runtime, javascript throws an "unterminated string" error because the newline is still there <script type="text/javascript> myFunction('line1<br /> line2'); </script> Using str_replace in conjunction with nl2br doesn't help: <?php str_replace("\n","",nl2br(htmlentities($_POST["field"]))) ?> At runtime, the argument for myFunction is still 'line1<br /> line2' and the "unterminated string" error stands I thought that maybe the newline was not matched to "\n" and so str_replace wasn't doing anything, so in php I tried: <?php str_replace("\n","GOTCHA",nl2br(htmlentities($_POST["field"]))) ?> In Javascript, the output generates an unterminated string error: <script type="text/javascript> myFunction('line1<br /> GOTCHAline2'); </script> The newline is still there. nl2br inserts <br /> before the newline, and str_replace enters GOTCHA after the newline (but doesn't remove the newline). This is driving me nuts. Any other suggestion? Quote Link to comment Share on other sites More sharing options...
NamemeNick Posted April 23, 2009 Author Share Posted April 23, 2009 SOLVED. Thank you guys; using nl2br in conjunction with str_replace was key. The correct handling of the post data is: <?php $input=str_replace("\r\n","",nl2br(htmlentities($_POST["field"]))); ?> Using "\r\n" instead of "\n" as the first argument of str_replace made the difference, but I don't understand why. Quote Link to comment Share on other sites More sharing options...
Bauer418 Posted April 23, 2009 Share Posted April 23, 2009 @premiso: Please read the requirements before telling people their solutions are wrong. He wanted to get rid of the newline character, not just add an HTML line break. @NamemeNick: The \r is a carriage return, the combination of \r\n usually results from a newline entered on a Windows OS. Linux uses simply \n to denote a newline. You have to make these extra checks to be sure, so I would change your code to: $input=str_replace(array("\r\n", "\n"),"",nl2br(htmlentities($_POST["field"]))); To be sure you capture both types of newlines. Quote Link to comment Share on other sites More sharing options...
NamemeNick Posted April 23, 2009 Author Share Posted April 23, 2009 Bauer418, that's fantastic advice. Thank you, well taken. Quote Link to comment Share on other sites More sharing options...
premiso Posted April 24, 2009 Share Posted April 24, 2009 Eh, my bad, long day. 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.