2

I have an array passed to a page via session:

Array(
[0] => Somewhere
Belfast
[1] => New York
)

The data is then gathered in a JS variable:

var json_university_addresses = '<?php echo (Session::get('university_address'))?>';

Before it is then split into a JS array:

var aUniversityAddresses = json_university_addresses.split(',');

The problem I am getting is the line break between 'somewhere' and 'belfast'. This is causing the page to fail.

Is there anything I can do with the data either side to make this easier? I want to display the line break to the user.

Thanks

rmccallum
  • 214
  • 3
  • 18

6 Answers6

5

Wouldn't it be better (read: easier) to use json_encode? Then you can format it on the client side.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • I have tried json encode/decode but since the array is not key/value it just returns a standard array. – rmccallum Aug 28 '12 at 12:13
  • The same issue occurs with the line break in the first iteration. The result is a JS error: unterminated string literal – rmccallum Aug 28 '12 at 12:29
  • Just a note as you are using Laravel. Send to your page using `Response::json(Session::get('university_address')` While laravel doesn't add much data to the string it does give it a common structure meaning you can write catch all JS functions without hand coding the JSON structure. – David Barker Aug 29 '12 at 08:46
0

Here is the solution...

<?php
    $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
    echo json_encode($arr);
 ?>
Hiren Soni
  • 574
  • 3
  • 11
0

You could try implode directly in the php code to output an array directly:

var aUniversityAddresses = [<?php echo implode(",", Session::get('university_address'))?>];

http://php.net/manual/en/function.implode.php

Bogdan
  • 43,166
  • 12
  • 128
  • 129
  • Sorry, the code should have hade implode around the session. This made no difference. – rmccallum Aug 28 '12 at 12:13
  • Or rather it did but gave a different error. As the line break was there the JS var thought the string was not terminated. – rmccallum Aug 28 '12 at 12:23
0

You should first escape newline characters before outputting data to javascript:

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

This way, your final code would be:

$universities = Session::get('university_address');
$universities_string = implode(',', $universities);
$universities_js = str_replace( array( "\n", "\r" ), array( "\\n", "\\r" ), $universities_string);

And then in the javascript file:

var json_university_addresses = "<?php echo $universities_js ?>";
Community
  • 1
  • 1
Corina
  • 1,475
  • 1
  • 12
  • 17
0

Ok I managed to get what I wanted:

var json_university_addresses = '<?php echo (Session::has('university_address')) ? preg_replace('/[\r\n]+/', "\\n",addslashes(implode(',',Session::get('university_address')) )) : "" ?>';

Thanks for the help. I did a quick search on here for unterminated string literals with JSON and got the preg replace statement above.

Thanks

rmccallum
  • 214
  • 3
  • 18
0

I think the line breaks are there in your php array so you should remove them from your session array first then use it in script

$sesion_array=Session::get('university_address');

$sesion_array=array_filter($sesion_array,'myFunction');

function myFunction($elem)
{
  return str_replace("\n","&nbsp;",$elem); 
}

then try

var json_university_addresses = '<?php echo $sesion_array ?>';
Sumit Neema
  • 462
  • 3
  • 18
  • But that will affect the display for the user in the textbox will it not? – rmccallum Aug 28 '12 at 13:58
  • if you don't want line breaks just remove them if it is already in your session array.it will not affect the display it will be displayed correctly just try it once if it works – Sumit Neema Aug 28 '12 at 14:03