Jump to content

[SOLVED] line breaks in textarea break my code


mapleleaf

Recommended Posts

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??

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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>");

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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>");

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.