Jump to content

[SOLVED] help replace newline with <br> in $_POST data


Recommended Posts

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?

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.

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?

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.

@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.

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.