SNT93
SNT93

Reputation: 476

json_encode() gives special charactors like '\n'..how can i replace them in PHP?

I encoded an array using json_encode() function and it gave me a string like this..

"[{"details":"power - 2000w \nac-220-240v \/ 50-60hz\n369 degree cordless base\n","model_id":"MC-EK3428 \/ MC-EK3328"}]"

as you can see it contains special characters like "\n"..I want these special characters to be replaced with "" because in javascript I am using the JSON.parse(); function to convert this string to an object.. but it gives me an error

syntaxerror : missing ) after argument list

I think this is because of the special characters in the string..how can I escape these?

Edit

php :
$view->jsonencoded_array = json_encode($array);
javascript :
var products = JSON.parse('<?php echo $jsonencoded_array; ?>');//this line gives me the error 

update :

found out that the error is given in this :

'<?php echo $jsonencoded_array; ?>'

Upvotes: 0

Views: 1921

Answers (3)

anand
anand

Reputation: 1526

There must something that you are missing or there is some other reason for your issue while parsing in JavaScript; because json_encode handles \n and other special characters such " \ etc. very well and escape them properly without any explicit work.

I would suggest you to check the JSON produced and you are supplying to JavaScript and see if there is something missing in between.

Note: You can do a str_replace but it is not advised. Better stick to json_encodesince its s standard function and it works well.

Edit: You should be echoing $view->jsonencoded_array not just $jsonencoded_array, no need to parse already JSON object.

php :

$view->jsonencoded_array = json_encode($array);

javascript :

var products = <?php echo $view->jsonencoded_array; ?>;

Upvotes: 1

SNT93
SNT93

Reputation: 476

json_encode() twice helped me to solve this issue..

$view->jsonencoded = json_encode(json_encode($array));

Upvotes: -1

Quentin
Quentin

Reputation: 943591

The problem here is that \n (and various other combinations) have special meaning inside a JavaScript string, and you are dumping your JSON into a JavaScript string without doing any conversion of those characters.

Since JSON is heavily inspired by JavaScript literal syntax, you can use json_encode to convert a PHP string into a JavaScript string.

There are some gotchas, the main one being that </script> can appear in a JSON text without causing any problems, but having that in the middle of your JavaScript <script> element is going to cause the HTML parser to cut off your JavaScript in the middle of the string … but PHP's default encoding rules will generate <\/script> which solves that problem.

So:

<?php
$json_array = json_encode($array);
$javascript_string = $json_encode($json_array);
?>
var products = JSON.parse(<?php echo $javascript_string; ?>);

That said. A JSON array is also a JavaScript array, so you can skip that step entirely.

<?php
$json_array = json_encode($array);
?>
var products = <?php echo $json_array; ?>;

Upvotes: 2

Related Questions