mapleleaf Posted May 4, 2009 Share Posted May 4, 2009 I have a textarea that has no editor attached so just a simple textarea. If a user makes a line break i can't get rid of the \n from my code output from the database. I have tried nl2br() but it still doesn't work The content of the textarea goes into mysql and out before being echoed out. I also tried htmlentities() For more info this is being sent into a google maps marker Firebug says: unterminated string literal [break on this error] var marker = createMarker(point,'<...:450px">My sale<br>this and this <br />\n How did that"\n" get inthere when i ran it through nl2br()?? Any ideas?? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Can you post your code and tell me where it breaks? Quote Link to comment Share on other sites More sharing options...
mapleleaf Posted May 4, 2009 Author Share Posted May 4, 2009 var marker = createMarker(point,'<div style="width:400px height:450px"><?php echo $loc; ?><br><?php echo nl2br($description); ?><br><?php echo $street; ?><br><?php echo $city; ?><br><?php echo $state; ?> <?php echo $zip; ?><br><?php echo $phone; ?><br><b>Start: </b><?php echo $start; ?><br><b>End: </b><?php echo $end; ?><br></div>'); It just breaks in this line for the google maps. If i echo nl2br($description); further down the page no problem. Quote Link to comment Share on other sites More sharing options...
mapleleaf Posted May 4, 2009 Author Share Posted May 4, 2009 just to clarify echo nl2br($description); gives: hello there echo $description; gives: hello there Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Try storing the second parameter as a variable and then pass it. I mean if the description has a ' quote in it, it'll break your statement. Example: $description = "how's your day?"; That ' before the s would break your string. Quote Link to comment Share on other sites More sharing options...
mapleleaf Posted May 4, 2009 Author Share Posted May 4, 2009 It doesn't have any quotes in it and I have tried htmlentities($description,'ENT_QUOTES') and still the problem is there Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 What if you did: echo str_replace('\\', '\', $description); ? Quote Link to comment Share on other sites More sharing options...
mapleleaf Posted May 4, 2009 Author Share Posted May 4, 2009 Good idea but unfortunately nope didn't work. This is way odd isn't it!! Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 How did you store $description anyways? Can I see more of the code? Say you have: <textarea name="blah"></textarea> What did you do with $_POST['blah']? Did you just store it into $description? Did you do anything else with it? Quote Link to comment Share on other sites More sharing options...
mapleleaf Posted May 4, 2009 Author Share Posted May 4, 2009 Yes it went through codeigniter's xss_clean and into a mysql database. It does no alteration to the text unless there is something like <script> or any other potentially bad input. What we are working with is the output of the query. This is what goes in <?php $this->form_validation->set_rules('description', ' Description', 'required|xss_clean'); if ($this->form_validation->run() == TRUE) { $inputs = array( 'title' => $_POST['title'], 'description' => $_POST['description'], 'phone' => $phone, 'email' => $_POST['email'], 'street' => $_POST['street'], 'city' => $_POST['city'], 'state' => $_POST['state'], 'zip' => $_POST['zip'], //get the right format here 'start' => $start, 'end' => $end, 'date_added' => time() ); $this->db->insert('sales', $inputs); } ?> if that helps and then just a normal query to get the data out. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Can you try doing: <textarea name="description"><?php echo $_POST['description']; ?></textarea> ? Would that print okay? Quote Link to comment Share on other sites More sharing options...
mapleleaf Posted May 4, 2009 Author Share Posted May 4, 2009 Works as it should Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Okay, so how are you defining the variable $description? Can you post that bit? Quote Link to comment Share on other sites More sharing options...
mapleleaf Posted May 4, 2009 Author Share Posted May 4, 2009 $description = $row->description; Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 What about $description = stripslashes($row->description);? Quote Link to comment Share on other sites More sharing options...
mapleleaf Posted May 4, 2009 Author Share Posted May 4, 2009 no slashes there but i tried it anyway and no luck Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted May 4, 2009 Share Posted May 4, 2009 The \n is a visual representation of a newline. nl2br converts these characters into line break tags ( <br /> ). However nl2br will not remove these characters. The true problem actually lies within your JavaScript. Escape characters such as \n only work within double quotes, not single quotes. You should use the following instead when defining your maker variable var marker = createMarker(point,"<div style=\"width:400px height:450px\"><?php echo $loc; ?><br><?php echo nl2br($description); ?><br><?php echo $street; ?><br><?php echo $city; ?><br><?php echo $state; ?> <?php echo $zip; ?><br><?php echo $phone; ?><br><b>Start: </b><?php echo $start; ?><br><b>End: </b><?php echo $end; ?><br></div>"); Quote Link to comment Share on other sites More sharing options...
mapleleaf Posted May 4, 2009 Author Share Posted May 4, 2009 Wildteen88 That didn't work but maybe there is a way i can get rid of the \n and replace them with<br> myself. The problem is that they don't seen to be in the string so can't be manipulated. Odd as they obviously are in the string I may have to use a text editor like tinymce or fckeditor which I was hoping to avoid Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 What happens to $_POST['description'] value before it gets inserted into the DB? Quote Link to comment Share on other sites More sharing options...
mapleleaf Posted May 4, 2009 Author Share Posted May 4, 2009 i just tested it bypassing all the validation and the same result. In case it is relevant: <?php $inputs = array( 'title' => $_POST['title'], 'description' => $_POST['description'], 'phone' => $phone, 'email' => $_POST['email'], 'street' => $_POST['street'], 'city' => $_POST['city'], 'state' => $_POST['state'], 'zip' => $_POST['zip'], //get the right format here 'start' => $start, 'end' => $end, 'date_added' => time(), 'paid' => 'n' ); $this->db->insert('sales', $inputs); ?> codeigniter everything is cleaned of course normally but it happened even without cleaning Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Does anything happen at $this->db->insert() method? Quote Link to comment Share on other sites More sharing options...
mapleleaf Posted May 4, 2009 Author Share Posted May 4, 2009 Cogeigniter generates the query and I suspect you have found the problem. I am sure it makes sure the query is safe. I will look at the class. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted May 4, 2009 Share Posted May 4, 2009 something like this? function removeLineBreaks($input) { $input = preg_replace('/(?<!\n)\n(?!\n)/', "<br />", $input); $output = str_replace("\r", "", $input); return $output; } usage: var marker = createMarker(point,"<div style=\"width:400px height:450px\"><?php echo $loc; ?><br><?php echo removeLineBreaks($description); ?><br><?php echo $street; ?><br><?php echo $city; ?><br><?php echo $state; ?> <?php echo $zip; ?><br><?php echo $phone; ?><br><b>Start: </b><?php echo $start; ?><br><b>End: </b><?php echo $end; ?><br></div>"); Quote Link to comment Share on other sites More sharing options...
lispwriter Posted May 4, 2009 Share Posted May 4, 2009 I've had to write my own newline to br code - this last post should work. The issue is you have to replace both "\n" and "\r" characters. str_replace should work for both. Just replace one of the two with <br> and the other with an empty string. Quote Link to comment Share on other sites More sharing options...
mapleleaf Posted May 4, 2009 Author Share Posted May 4, 2009 mrMarcus I owe you. Thank you sooooo much!! Hours went into that and also thanks to Ken for endless attempts at solving it. Lipswriter knew all along 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.