PHP rawurlencode followed by JS decodeURIComponent gives malformed URI error

89 Views Asked by At

I have this piece of code that encodes an array of results from a database search:

foreach($searchResults[$i] as $key => $value) {
        $searchResults[$i][$key] = rawurlencode($value);

    }

I had to encode in order to pass the data as JSON to another page. On the other page, I'm trying to decode the resulting object like this in Javascript:

 if (results !== null) {
                    for (var i = 0; i < results.length; i++) {
                        $.each(results[i], function (key, value) {
                            results[i][key] = decodeURIComponent(results[i][key]);
                        });
                    }
                }

My problem is that I'm getting a URIError due to malformed URI. There are several pieces of data being passed, so my real question is if there is some method or tool that allows you to search an array of strings for the offending item. I have several results and don't relish the idea of having to go through them character by character to find the offending encoding. Does anyone have any suggestions? If I could figure out what character/characters is causing this error I would be on my way to figuring out a solution. I'm a coding newbie, so please forgive any incorrect use of terminology.

BTW - Sometimes this code works perfectly... I'm trying to find a way to narrow down the offending database items that are causing this error to occur periodically.

0

There are 0 best solutions below