json_encode() - how to get original data back? - php

I"m using the function json_encode() to encode my data in php before sending it back to my page. However, single quotes and double quotes have been escaped and appear as /' and /" in javascript. Any idea how to get back those quotes without those slashes in javascript. I'm using jquery.

Use $.parseJSON() if your JSON is a string.
If after that, you still have slashes, you may have magic quotes on. Check with this...
var_dump(get_magic_quotes_gpc());
If you get TRUE, disable magic quotes.

Related

Backslashes in posting data to a URL using CURL Php

I am trying to post some data to a URL which contains double quotes.I escaped the quotes by adding back slashes.I just wanted to clarify will that be a problem while posting data.
$data = "{\"phoneNumber\":\"$no\",\"campid\":\"$refer\",\"simcards\":[{\"srno\":\"89914904040576482763\",\"mcc\":404,\"mnc\":49,\"operatorName\":\"airtel\",\"circleName\":\"AndhraPr\",\"msisdn\":\"\"},{\"srno\":\"89914904040874441867\",\"mcc\":404,\"mnc\":49,\"operatorName\":\"airtel\",\"circleName\":\"AndhraPr\",\"msisdn\":\"\"}]}";
If I shouldnt use backslashes ,what is the alternative.
just use single quotes outside then you dont have to escape the doubles inside
$data = '{"phoneNumber":"'.$no.'","campid":"'.$refer.'","simcards":[{"srno":"89914904040576482763","mcc":404,"mnc":49,"operatorName":"airtel","circleName":"AndhraPr","msisdn":""},{"srno":"89914904040874441867","mcc":404,"mnc":49,"operatorName":"airtel","circleName":"AndhraPr","msisdn":""}]}';

Remove multiple quotes in json encoded array

is it possible to strip double quotes from json encoded array?
Exaample:
{"96":{"term_id":96,"name":"AS Roma","slug":"as-roma","term_group":0,"term_taxonomy_id":100,"taxonomy":"klient","description":"HWDP","parent":0,"count":5,"object_id":1800,"filter":"raw"},"100":{"term_id":100,"name":"Atletico Madrid","slug":"atletico-madrid","term_group":0,"term_taxonomy_id":104,"taxonomy":"klient","description":""vamos amigos"","parent":0,"count":6,"object_id":1800,"filter":"raw"}}
This is an array of values from wordpress get_the_terms function. As you may see one of those values "description":""vamos amigos"" has doubled double quotes (the input text in description is inside quotes). Which gives me an error on ajax response. I've tried everything to strip this double quotes - nothing works for me?
I would appreciate any indications.

MySQL Data Into Value Attribute? (Quotes)

Alright, so there's still stuff I have yet to learn about PHP. I'm trying to retrieve data from a MySQLi database and it's all fine until I'm forced to choose between double quotes or single quotes breaking something. With real_escape_string, I can store string data that contains a single quote, and it just gets escaped with a backslash, but if I don't use stripslashes() when I insert it into the value attribute...
If my value attribute looks like this in the code: value="_" then double quotes within the string, trim any data after it because it seems to be interpretted as the end of the value attribute.
If my value attribute looks like this in the code: value='__' then if I don't use stripslashes(), I see the slashes in the output, and if I use stripslashes(), it's the same thing with the double quotes, but with any of the escaped single quotes within the string.
Hope this makes sense. I'm fairly tired right now, but with a few replies and questions asked for anyone who doesn't quite understand, I'm sure we can figure this out. :)
If you have to output data into html which might have special characters use htmlspecialchars
<input type="text" value="<?php echo htmlspecialchars('\'"&<>') ?>">
http://codepad.org/DxV3uq0L
http://jsfiddle.net/Uu29D/

Escaping/encoding single quotes in JSON encoded HTML5 data attributes

In PHP, I use json_encode() to echo arrays in HTML5 data attributes.
As JSON requires - and json_encode() generates - values encapsulated by double quotes. I therefor wrap my data attributes with single quotes, like:
<article data-tags='["html5","jquery","php","test's"]'>
As you can see, the last tag (test's) contains a single quote, and using json_encode() with no options leads to parsing problems.
So I use json_encode() with the JSON_HEX_APOS parameter, and parsing is fine, as my single quotes are encoded, but I wonder: is there a downside doing it like this?
You need to HTML escape data echoed into HTML:
printf('<article data-tags="%s">',
htmlspecialchars(json_encode(array('html5', ...)), ENT_QUOTES, 'UTF-8'));
or use the build-in option:
json_encode(array('html5', ...), JSON_HEX_APOS)
you can check it up in the manual: http://php.net/manual/en/json.constants.php#constant.json-hex-apos

issues with quotes in json string :php

I have tried lots of things and could not get through yet. I have a json string with some single quotes, double quotes and some other characters which are causing an error when I am trying to iterate the json by converting in into an array.
The quotes are escapes using addslashes and are going correctly in the database. But when I am converting the string to array using json_decode and stripslashes it says invalid argument passed to for loop.
Please suggest.
Thanks in advance.
Do this on the quotes:
$str = htmlentities($str, ENT_QUOTES);
And they will get-off your way.
As I remember, you have to first escape the quotes in php, and then escape the quotes and slashes when you print the json_encode. So, for "\"text\"" the output has to be \"\\"text\\"\".

Categories