2

I have a PHP string that contains an address and as such contains a few lines. One example could be:

$custDetails = "
123 Main Street
City
Area Code
";

Although this address is retrieved using an SQL query, not declared in PHP.

I would like to replace the go to line characters by something else, temporarily, to make modifying this string easier in another javascript function. But I'm running in to some problems. This code:

echo "
<script>
  if ('$custDetails'.indexOf('\n') != -1)
     alert('yes');
  alert('$custDetails');
</script>
";

Becomes:

<script>
  if ('123 Main Street
City
Area Code'.indexOf('
') != -1)
  alert('yes');
  alert('123 Main Street
City
Area Code');
</script>

after treated by PHP. The "go to lines" in the string are treated as go to line in the script tag, and as such messes up the script. Additionally, I can't detect the \n char because it is treated as a go to line, and messes up the script...

How can I make a string with multiple lines usable in javascript?

EDIT: As suggested in one of the answers, I tried replacing "\n" with 'n' in PHP before calling the javascript

$custDetailsBis = str_replace("\n", '\n', $custDetails);

echo "<script>
alert(\"$custDetailsBis\");
</script>";

I still doesn't work, output in browser:

<script>
  alert("16 St Andrews Street
\nDundee
\nDD1 2EX");
</script>

Note that I know have \n that have appeared on the browser output, but there are still line breaks. Again the line breaks are causing errors in the javascript.

Juicy
  • 10,990
  • 33
  • 107
  • 196

4 Answers4

2

Try this:

$custDetailsEscaped = str_replace("\n", '\r\n', $custDetails);

Note the double quotes for the first argument and single quotes for the second - it's significant here.

Then replace all instances of $custDetails in your echo statement with $custDetailsEscaped:

$custDetailsEscaped = str_replace("\n", '\r\n', $custDetails); 

echo "
<script>
  if ('$custDetailsEscaped'.indexOf('\\r\\n') != -1)
     alert('yes');
  alert('$custDetailsEscaped');
</script>
";

Output:

<script>
  if ('\r\n123 Main Street\r\nCity\r\nArea Code\r\n'.indexOf('\r\n') != -1)
     alert('yes');
  alert('\r\n123 Main Street\r\nCity\r\nArea Code\r\n');
</script>
George Brighton
  • 5,071
  • 9
  • 27
  • 36
  • Perhaps I misunderstood your question. What output are you looking for on the page? – George Brighton Sep 16 '13 at 23:20
  • Hey thanks for the answer. No actually I want the variable to be evaluated. That is not the problem. If you check the code I posted the problem is that the multi line string (again that I want evaluted) contains line breaks. These line breaks mess up the javascript, as you can see in the browser source snippet I posted. – Juicy Sep 16 '13 at 23:21
  • Note the double quotes on the first argument, and single quotes on the second: `str_replace("\n", '\n', $custDetails)` – George Brighton Sep 16 '13 at 23:45
  • Thanks for the help but it's still not working! I've updated the EDIT part of my question with your suggestion, and the output it produces! – Juicy Sep 16 '13 at 23:54
  • Hmm... you must be using some strange line endings. What happens if you paste the contents of this (http://pastebin.com/v0rrYzL5) into a new PHP file and run it? For me it outputs the correctly escaped alert - `alert("\n123 Main Street\nCity\nArea Code\n");` - surrounded by ` – George Brighton Sep 17 '13 at 00:01
  • Interesting... could it be Firefox? I copy pasted your code in fresh PHP file. I get the same problem with your code. In the source, each \n produces a new line, and messes up the javascript. EDIT: I confirm it works in Chrome, it all appears on the same line. Annoying problem.... – Juicy Sep 17 '13 at 00:06
  • Must that Firefox and Chome treat line endings differently. You could try CR+LF (`\r\n`) instead... – George Brighton Sep 17 '13 at 00:11
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37488/discussion-between-juicy-and-george-brighton) – Juicy Sep 17 '13 at 00:13
1

You can use json_encode which should automatically replaces new lines with \r\n.

json_encode($custDetails);
Giraldi
  • 15,571
  • 4
  • 31
  • 49
0

You can use a str_replace() to change the End Of Line buy empty string.

This post may help , @elusive uses in his answer regex to replace the end of lines.

Community
  • 1
  • 1
S.Thiongane
  • 6,803
  • 3
  • 36
  • 52
0

Before echoing the var from php you should replace \n with \\n so javascript will handle it as \n.

Pep Lainez
  • 959
  • 7
  • 12
  • Hey, thanks for you answer. I updated my question with what I tried to implement your solution. Still can't get it to work! – Juicy Sep 16 '13 at 23:34