Asked  7 Months ago    Answers:  5   Viewed   26 times

Possible Duplicate:
Why are escape characters being added to the value of the hidden input

So, I have a file called Save.php.

It takes two things: a file, and the new contents.

You use it by sending a request like '/Resources/Save.php?file=/Resources/Data.json&contents={"Hey":"There"}'.

..but of course, encoding the url. :) I left it all unencoded for simplicity and readability.

The file works, but instead of the contents being..

{"Hey":"There"}

..I find..

{"Hey":"There"}

..which of course throws an error when trying to use JSON.parse when getting the JSON file later through XHR.

To save the contents, I just use..

file_put_contents($url, $contents);

What can I do to get rid of the backslashes?

 Answers

78

Turn magic_quotes off in PHP.ini.

Wednesday, March 31, 2021
 
Viralk
answered 7 Months ago
19

What a HORRENDOUS debug session.. well there's good news.. I figured it out..

I started looking at it using AJAX and logging it with Firebug... and it turns out json_decode (or eval by the way) cannot handle ", which is what PHPUnit sends back (Come on Sebastian!), so to fix it:

$json = str_replace('"', '"', $json);

Now I thought they were the same.. maybe someone can enlighten me..

Wednesday, March 31, 2021
 
WooDzu
answered 7 Months ago
41

Here's another way.

This function increments in base( [number of elements in array] )

and uses the strtr function to swap out the characters for strings.

function everyCombination($array) {

    $arrayCount      = count($array);
    $maxCombinations = pow($arrayCount, $arrayCount);
    $returnArray     = array();
    $conversionArray = array();

    if ($arrayCount >= 2 && $arrayCount <= 36)
    {
        foreach ($array as $key => $value) {
            $conversionArray[base_convert($key, 10, $arrayCount)] = $value;
        }

        for ($i = 0; $i < $maxCombinations; $i++) {
            $combination    = base_convert($i, 10, $arrayCount);
            $combination    = str_pad($combination, $arrayCount, "0", STR_PAD_LEFT);
            $returnArray[]  = strtr($combination, $conversionArray);
        }

        return $returnArray; 
    }

    echo 'Input array must have between 2 and 36 elements';
}

Then ...

print_r(everyCombination(array('a', 'b', 'c', 'd')));

This also seems to be significantly faster than the recursive example below.

Using microtime() on my server this code runs in 0.072862863540649 seconds

The recursive example below takes 0.39673089981079 seconds.

138% faster!

Wednesday, March 31, 2021
 
Gerardo
answered 7 Months ago
95

It takes a great deal of care to get a C program correct and secure. That care means that you need to have really good people writing your programs. That means you pay more.

Also, C doesn't have the benefit of drawing from an enormous single standard library of functionality as .NET (and the other major web-centric platforms) has. So you may have to either buy components, or perform interop, or roll your own functionality which comes "for free" with a more, shall we say "web-centric" language like PHP or C# or Ruby or whatever. That means you pay more.

Add all of that to the fact that single-threaded computational speed just isn't that important on the web. If you need more scalability, most organizations can economically just throw more cores at the problem and be fine. This is not true for everyone, of course. I'd imagine that the core of Google's engine is written in C or a similar language not only for speed, but to save real money in power costs.

Saturday, July 31, 2021
 
Zigglzworth
answered 3 Months ago
99

Debugging suggestion:

Check the output of json_last_error(). It should give you an exact reason why it doesn't work. Available from PHP 5.3.0 only, though.

The reason:

JSONP is not identical with JSON. It contains extra data that breaks json_decode().

Solution:

Remove the extra brackets using substr($AVDecode, 1, strlen($AVDecode)-2)

Friday, October 22, 2021
 
Zed
answered 2 Days ago
Zed
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :