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.
Related
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/
Normally, when variables in PHP are enclosed in single quotes, they are treated as strings, i.e
echo '$variable';
will actually echo the word $variable onto the screen.
So why is it then that this string is parsed:
echo "'$variable'";
That code actually does echo the value of the variable. Why is that? It's still inside single quotes, so why does it still get parsed?
The string is wrapped in double quotes -- the single quotes are part of the content of the string, not part of the string's delimiter. Therefore the single quotes have no semantic meaning whatsoever.
Your question indicates that you may have a fundamental misunderstanding of strings. This is OK! Strings are surprisingly complex entities, and will only get more complex if you learn lower level languages like C. I would suggest you spend some time reading up on strings both in general as well as within PHP. A few quick google searches will honestly be better than a curated list for this task.
Because the single quotes are inside double quotes. Anything inside double quotes gets evaluated. So, your echo statement is passed a string inside double quotes.
This string is evaluated then output. It contains single quotes and a variable.
Try this instead:
<?php
$var = 10;
echo '"$var"';
?>
Because it's in double-quotes as well. The outer most layer of quotes denotes what kind of string it is.
It is simply a double quoted string that contains two single quote characters. Once they are in the double quotes, they have no meaning to the parser.
I am trying to use an editable table and have it working except for when the array values passed to the save function contain double quotes. The error occurs at foreach loop
foreach($saveArray as $rowId=>$row) {
It is the values (not keys) which may contain double quotes, the actual error being:
Warning: Invalid argument supplied for foreach()
What is the best way around this, some way to escape them, change them to the " code, change the way the loop works?
EDIT:
Sorry, the problem is actually with the json_decode function and double quote values, not returning an array.
Works fine for non double quoted entries
json looks like
{"2":{"component":"8\"", ...
So it is escaped but it's not decoding into an array
See what $saveArray actually is, using
var_dump($saveArray)
It doesn't look like your $saveArray, whatever it is, supports the foreach construct.
If $saveArray comes from json_decode(), it's likely that your JSON string is invalid, and json_decode() just returns NULL.
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.
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\\"\".