10

I have code like this:

<?php
echo '<script type="text/javascript">';
echo 'var out="'.$txt.'";';
echo '</script>';
?>

Where $txt is a PHP variable that can contain newlines like this:

line1
 line2 hello world

Which would end up like this:

var out="line1
 line2 hello world";

Which will cause a Javascript error, of course.

What is the best way to handle this? The out variable will be used in a HTML textarea, so I don't think it can be parsed into <br>

Zeno
  • 1,769
  • 7
  • 33
  • 61

6 Answers6

38
$txt = str_replace( array( "\n", "\r" ), array( "\\n", "\\r" ), $txt );

should replace newlines. Don't do it this way.

This is a naïve implementation of string escaping for JavaScript. As you're actually trying to format a string for use in JavaScript, a much better solution would be to use json_encode:

$txt = json_encode($txt);
echo "<script>var out={$txt};</script>";

json_encode will correctly escape special characters in strings, such as quotes, tabs, form feeds, and other special unicode characters. It will also perform all the correct escaping for converting objects, arrays, numbers, and booleans.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • textarea doesn't parse \n though, it's showing up as "\n" in the actual textarea. – Zeno Nov 24 '10 at 19:41
  • are you triple escaping `\n`? In JavaScript `"\n"` will be a newline, but if you pass it `"\\n"` you'll get `\n` as output. Check your page source and see what the JavaScript code is coming out as. – zzzzBov Nov 24 '10 at 19:44
  • Your question was how to handle it for Javascript. Don't run str_replace on it if you're putting it into a textarea. – Jonah Nov 24 '10 at 20:29
  • Textarea newline is a whole other issue, cant do it in placeholder for textarea without major hacks – Brock Hensley Jun 24 '13 at 19:10
  • @dirt, what do you mean by that? Do you mean you want the placeholder attribute of a textarea to span multiple lines? – zzzzBov Jun 24 '13 at 19:16
  • 1
    Just letting everyone like @Zeno know that the textarea placeholder attribute does not support new lines at all, you can do it with value though. – Brock Hensley Jun 24 '13 at 20:51
  • So easy if you know it :) – Camathon Apr 28 '19 at 03:48
4

you can add a \ at the end of a line to create a multi line String

var out="line1 \
 line2 hello world";
Daniel
  • 2,331
  • 3
  • 26
  • 35
1

Most of these don't work for me. Normally, I'd use json_encode like

    <?php
        $MyVar = json_encode($MyVar);
    ?>
    <javascript language='javascript'>
        MyVar = <?php echo $MyVar; ?>

But for a quick fix you can just break a line like this: Pay attention to the double quotes.

    <?php
    $MyVar = "line one here
              then line two here


              finally line five here";

    //** OR

    $MyVar = $MyVarA . 
    "

    " 
    . $MyVarB;

    ?>
    <HTML>
    <HEAD>
    <javascript language='javascript'>
    Myvar = "<?php echo $MyVar; ?>";

. . .

MAtkins
  • 53
  • 1
  • 8
0

I tried this and it worked well.

<?php
    echo '<script type="text/javascript">';
    $txt = "line1 \\n line2 hello world";
    echo 'var out="'.$txt.'";';
    echo '</script>';
?>

I am using PHP 5.3

Frits
  • 7,341
  • 10
  • 42
  • 60
Dinesh Patra
  • 1,125
  • 12
  • 23
0
$content = str_replace( "\\n", "\\\\\\n", $content );

Result:

var a = "Hello \
World"
Roman
  • 3,799
  • 4
  • 30
  • 41
0

You can use str_replace to convert line breaks into a different character (in this case, perhaps a space, but it depends how you want the output to show up)

$out = str_replace("\n", '\n', $in);
Dave Kiss
  • 10,289
  • 11
  • 53
  • 75