Php json_encode not escaping newlines

I ran into json_encode problem.

when I json_encode an array having new lines, does not escape new lines, but deletes \ and saves n.

ex: $array = array('name'=> "some text \n\r text"); $results = json_encode($array); 

it saves some text nr text in the database.

I am using php 5.3.8 .

change

This is my original code that I use

 $attr = array(); for($i=0; $i < count($_POST['key']); $i++){ $attr[$_POST['key'][$i]] = $_POST['value'][$i]; } echo json_encode(array('custom' => $attr)); 

these are the POST values ​​obtained from the form.

+4
source share
5 answers

I understood this problem. this is not a json_encode problem. This is an error when saving to the database.

The problem is magic_quotes_gpc enabled on the server. in my application, if magic_quotes is enabled, I break the slashes.

i disabled magic_quotes_gpc . now works great.

Thanks for every body.

+1
source

Newlines are not valid characters inside JSON strings . This is the expected behavior:

char

any Unicode character except "or \ or control character

  • \ "
  • \
  • /
  • \ B li>
  • \ e
  • \P
  • \ g
  • \ t
  • \ u four-digit numbers

JSON removes these control characters from the ones listed.

So now we have '\n' (literally the backslash followed by 'n'), which, if not executed properly, will be stored in the database as n . And , what is the problem you are facing.

Decision

Use the prepared instructions to correctly delete all and all slashes in the lines that you store in your database. This will save them as '\n' , which you can convert to "\n" when you extract your data.

+9
source

You can manually avoid them:

 $array = array('name'=> "some text \n\r text"); $results = json_encode(array_filter($array, function($arr) use ($array){ return preg_replace('~\\[nrtfd]~', '\\\\$1', $arr); })); print_r($results); 

You can extend your own json_encode function and replace using json_encode with my_json_encode :

 function my_json_encode($json){ $results = json_encode(array_filter($json, function($arr) use ($array){ return preg_replace('~\\[nrtfd]~', '\\\\$1', $arr); })); return $results; } print_r($results); 

The FYI above returns: {"name":"some text \n\r text"} instead of {"name":"some text nr text"}

0
source

You can use PHP_EOL for a new line. Where to include a new line depends on how you want. In the following case, I need a new line after the last closing square bracket and each curly brace:

 tit1: { "prop1" : [ "", "", []], "prop2" : [ "", "", []] }, tit2: { "prop1" : [ "", "", []], "prop2" : [ "", "", []] } 

Function

 $jsonDataTxt = $this->own_format_json($jsonData); file_put_contents("C:/Users/mm/Documents/Data.json", $jsonDataTxt); function own_format_json($json, $html = false) { $tabcount = 0; $result = ''; $bnew = 0; $brack=0; $tab = "\t"; $newline = PHP_EOL; for($i = 0; $i < strlen($json); $i++) { $char = $json[$i]; if($char!==',' && $bnew===1) { $bnew=0; $result.= $newline; } //insert new line after ], which is not proceeded by , switch($char) { case '{': $tabcount++; $result .= $char . $newline . str_repeat($tab, $tabcount); break; case '}': $tabcount--; $result = trim($result) . $newline . str_repeat($tab, $tabcount) . $char . $newline; break; case '[': $brack++; $result .= $char;// . $newline . str_repeat($tab, $tabcount); break; case ']': $brack--; $result .= $char;// . $newline . str_repeat($tab, $tabcount); if($brack===0) { $bnew=1; } //echo "<br><br> case ] char=".$char.', brack='.$brack. ", bnew=".$bnew.", result=".$result ; break; case ',': $result .= $char;// . $newline . str_repeat($tab, $tabcount); if($bnew===1) { $bnew=0; $result.= $newline; } //insert new line after ], break; case '"': $result .= $char; break; default: $result .= $char; } } return $result; } 
0
source

I do not believe json_encode is your problem. My guess is that your database interprets \ as an escape character, so it just removes them.

To combat this, just avoid escape characters using addslashes :

 $results=addslashes($results); 
-1
source

All Articles