1

I have a query string with URL-encoded symbols:

$wm_string = "LMI_MODE=1&LMI_PAYMENT_DESC=%CF%EE%E6%E5%F0%F2%E2%EE%E2%E0%ED%E8%E5+Plan+Z";

I need to convert it into JSON with PHP, but json_encode returns an empty string.

Here is my code in PHP:

parse_str($wm_string, $_REQUEST);
var_dump($_REQUEST);
echo "JSON:".json_encode($_REQUEST);

Here is the result:

array(1) { ["LMI_MODE"]=> string(46) "1?LMI_PAYMENT_DESC=Пожертвование Plan Z Online" } JSON:

What should I do?

UPDATE:

The expected result is:

{ 
    "LMI_MODE":1,
    "LMI_PAYMENT_DESC":"Пожертвование Plan Z Online" 
}

UPDATE2: The encoding is windows-1251, while json_encode seems to be expecting UTF-8. Is there a way to tell json_encode which encoding it should use while parsing?

5
  • Encode the url on the javascript side instead of the php side. I use json encode a lot and regularly send urls. Never bothered to encode them first as it doesn't make sense in my use case. You could probably find a way around it as well. Commented Jul 8, 2015 at 15:08
  • I can't because it is done by the external payment system. Commented Jul 8, 2015 at 15:11
  • 1
    check json_last_error(). There's no way it should fail to encode such a simple array. Commented Jul 8, 2015 at 15:21
  • Malformed UTF-8 characters, possibly incorrectly encodedю The form posts in Windows 1251. Commented Jul 8, 2015 at 15:29
  • Ah ok. If it's a 3rd party posting in the wrong encoding then yes you need to iconv everything. I will update my answer. Commented Jul 9, 2015 at 7:14

3 Answers 3

5

Since json_encode does work with UTF-8 only, and the text is in windows-1251, it should be converted from that encoding to UTF-8.

$wm_string = "LMI_MODE=1&LMI_PAYMENT_DESC=%CF%EE%E6%E5%F0%F2%E2%EE%E2%E0%ED%E8%E5+Plan+Z+Online";
$wm_string = iconv("windows-1251", "UTF-8", $wm_string);
parse_str(urldecode($wm_string), $result);
echo "JSON:".json_encode($result, JSON_UNESCAPED_UNICODE);

Output:

JSON:{"LMI_MODE":"1","LMI_PAYMENT_DESC":"Пожертвование Plan Z Online"}
Sign up to request clarification or add additional context in comments.

1 Comment

This is a bit confusing. Why are you saving back to the $_REQUEST variable? There is also no need to urldecode so many times.
0

Try this:

$wm_string = "LMI_MODE=1LMI_PAYMENT_DESC=%CF%EE%E6%E5%F0%F2%E2%EE%E2%E0%ED%E8%E5+Plan+Z";
$wm_string = (parse_url(urldecode($wm_string)));
$wm_string = json_encode(urlencode($wm_string['path']));
echo "JSON: " . $wm_string;

Result:

JSON: "LMI_MODE%3D1LMI_PAYMENT_DESC%3D%CF%EE%E6%E5%F0%F2%E2%EE%E2%E0%ED%E8%E5+Plan+Z"

1 Comment

I think you misunderstood the question. I have added the expected result of json_encode output in my original post.
0

I know this is similar to the other two answers, but just wanted to point out that you don't need to and in fact shouldn't be using urldecode with parse_url. In a simpler approach you could parse the url string and convert the windows-1251 variable to utf8 like this.

<?php

$wm_string = "LMI_MODE=1&LMI_PAYMENT_DESC=%CF%EE%E6%E5%F0%F2%E2%EE%E2%E0%ED%E8%E5+Plan+Z+Online";

//Parse the url string
parse_str($wm_string, $args);

//Convert payment description to utf8
$args['LMI_PAYMENT_DESC'] = iconv("windows-1251", "UTF-8", $args['LMI_PAYMENT_DESC']);

echo "JSON: " . json_encode($args, JSON_UNESCAPED_UNICODE);

//JSON: {"LMI_MODE":"1","LMI_PAYMENT_DESC":"Пожертвование Plan Z Online"}

2 Comments

What if there was another variable needing conversion? Then should we iconv it as well? Originally I didn't know what to expect to see in $_POST and tried to save it to a file via json_encode. That turned out to be a very bad idea and I should've used var_export instead, but this is another story.
Are you reading from $_POST? So is your $wm_string variable just for convenience to ask the question? You don't even need parse_str in that case since you're not parsing a string. But yes you could loop all items or take a look at fixing the issue at source which is probably the character encoding on your page/form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.