PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 27, 2022

[FIXED] php json_encode not escaping new lines

 January 27, 2022     json, php     No comments   

Issue

I am facing some issue with json_encode.

when i json_encode an array having new lines it is not escaping new lines instead it's removing \ and keeping n.

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

it is saving some text nr text in database .

i am using php 5.3.8.

edit:

This is my original code i am using

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

these POST values getting from form.


Solution

I figured out this issue. it is not in json_encode problem. it is an error while saving to database.

The problem is magic_quotes_gpc enabled in server. in my application if magic_quotes enabled i am striping the slashes.

i disabled magic_quotes_gpc. working fine now.

Thanks for every body.



Answered By - sankar.suda
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing