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

3
  • why Session::get('university_address') instead of $_SESSION['university_address']? Commented Aug 28, 2012 at 12:06
  • It's Laravel. Session::get is the notation to use. Commented Aug 28, 2012 at 12:12
  • @dnagirl: He probably has a wrapper class to access session vars. But that's not really affecting the question asked Commented Aug 28, 2012 at 12:13

6 Answers 6

5

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

Sign up to request clarification or add additional context in comments.

3 Comments

I have tried json encode/decode but since the array is not key/value it just returns a standard array.
The same issue occurs with the line break in the first iteration. The result is a JS error: unterminated string literal
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.
0

Here is the solution...

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

Comments

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

2 Comments

Sorry, the code should have hade implode around the session. This made no difference.
Or rather it did but gave a different error. As the line break was there the JS var thought the string was not terminated.
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 ?>";

Comments

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

Comments

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 ?>';

2 Comments

But that will affect the display for the user in the textbox will it not?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.