7

I have text that comes from a textarea where the user might have legitimate carriage returns.

If the text has a carriage return that I want to preserve, how do I do that with this exact jQuery append example.

I'm generating this html code and it will break if the variable $text has carriage returns in it.

Example:

$(".inner").append( "<textarea><?php echo $text; ?></textarea>" );

So if:

$text = "Cat

Dog";

Then I get this and it causes an error.

$( ".inner" ).append( "<textarea>Cat

Dog</textarea>" );

What can I do to preserve the carriage returns when append creates this textarea html?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Chain
  • 597
  • 2
  • 8
  • 24
  • Possible duplicate: http://stackoverflow.com/questions/508269/how-do-i-break-a-string-across-more-than-one-line-of-code-in-javascript – hindmost Aug 04 '15 at 19:31
  • @CodeGodie AFAIK `htmlentities` doesn't encode line-break, which is legal character in HTML – hindmost Aug 04 '15 at 19:37
  • 1
    You could encode carriage returns as
    , although that might mangle formatting.
    – Michael McPherson Aug 04 '15 at 19:51
  • 1
    @MichaelMcPherson You cannot use an html tag like `
    ` in a textarea, you need a `\n` to get a new line there.
    – jeroen Aug 04 '15 at 19:53
  • Can you not, @jeroen? Huh. Not my strength, clearly. Although this has an interested possible solution: http://stackoverflow.com/questions/8627902/new-line-in-text-area – Michael McPherson Aug 04 '15 at 19:58

4 Answers4

2

You need to make sure that the php value gets passed to javascript correctly. The best way to do that, is to encode it as json. Then you can use for example .val() to set the value of your element:

var myText = <?php echo json_encode($text); ?>;
$( ".inner" ).append( $('<textarea>').val(myText) );
jeroen
  • 91,079
  • 21
  • 114
  • 132
0

Use addcslashes function, it allows to encode carriage returns as \n (which is compatible with javascript).

$(".inner").append( "<textarea><?php echo addcslashes($text,"\r\n"); ?></textarea>" );

Function reference

Update

I decline my answer for the @jeroen 's answer. In case you have an old PHP version which doesn't support json_encode you can use Chengings' function

function escape_javascript_string($str){
    // if php supports json_encode, use it (support utf-8)
    if (function_exists('json_encode')) {
        return json_encode($str);
    }
    // php 5.1 or lower not support json_encode, so use str_replace and addcslashes
    // remove carriage return
    $str = str_replace("\r", '', (string) $str);
    // escape all characters with ASCII code between 0 and 31
    $str = addcslashes($str, "\0..\37'\\");
    // escape double quotes
    $str = str_replace('"', '\"', $str);
    // replace \n with double quotes
    $str = str_replace("\n", '\n', $str); 
    return $str;
}
davcs86
  • 3,926
  • 2
  • 18
  • 30
0

All you need to do is the following:

PHP:

$cleaned = str_replace("\r\n", "\\n", $text);

JS:

$(".inner").append("<textarea><?= $cleaned ?></textarea>");
CodeGodie
  • 12,116
  • 6
  • 37
  • 66
  • PHP_EOL is the line break character(s) used by the server platform. You can't just assume that the source of the text is the same platform. – davcs86 Aug 04 '15 at 20:02
0

This example from php.net documentation is working for me now. From Replace carriage return in an email

// Order of replacement
$order   = array("\r\n", "\n", "\r");
$replace = '<br />';

// Processes \r\n's first so they aren't converted twice.
$newstr = str_replace($order, $replace, $text);

The "br" tag is working inside of the textarea, though I though it wouldn't.

Community
  • 1
  • 1
Chain
  • 597
  • 2
  • 8
  • 24