Php parses the json string from HTMLpost

Source: Internet
Author: User
Tags javascript eval
Php parses the json string from HTMLpost. I convert a json object into a json string in js, and then submit it to php in an implicit input.
This is the HTML part.
 

The string obtained in php is:

[{\\"table\\":\\"a\\",\\"field\\":\\"value\\",\\"max\\":60,\\"min\\":null}]

String Processing
$ Json_string = $ _ POST ['json']; $ json = htmlspecialchars_decode ($ json_string); print_r (json_decode ($ json); // The result is null.


Change
$ Json = stripslashes (htmlspecialchars_decode ($ json_string); print_r (json_decode ($ json); // The result is still null.


Change it.
$ Json = stripslashes (htmlspecialchars_decode ($ json_string); print_r (json_decode ($ json); // well, the result is still blank.




Reply to discussion (solution)

It's really hard for you to do so complicated coding.

$s = '[{\\"table\\":\\"a\\",\\"field\\":\\"value\\",\\"max\\":60,\\"min\\":null}]';$s = html_entity_decode($s);$s = stripslashes($s);print_r(json_decode($s, 1));
Array(    [0] => Array        (            [table] => a            [field] => value            [max] => 60            [min] =>         ))

$str='[{\\"table\\":\\"a\\",\\"field\\":\\"value\\",\\"max\\":60,\\"min\\":null}]';$new=htmlspecialchars_decode($str);$new=str_replace('\\','',$new);$new1=json_decode($new,true);echo "
";print_r($new1);echo "
";

Array
(
[0] => Array
(
[Table] =>
[Field] => value
[Max] => 60
[Min] =>
)

)

It's really hard for you to do so complicated coding.

$s = '[{\\"table\\":\\"a\\",\\"field\\":\\"value\\",\\"max\\":60,\\"min\\":null}]';$s = html_entity_decode($s);$s = stripslashes($s);print_r(json_decode($s, 1));
Array(    [0] => Array        (            [table] => a            [field] => value            [max] => 60            [min] =>         ))



Or not.
However, in php, I directly write $ json_string = '[{\ "table \": \ "a \", \ "field \\": \ "value \", \ "max \": 60, \ "min \": null}] '; yes, is the data in POST different?

$str='[{\\"table\\":\\"a\\",\\"field\\":\\"value\\",\\"max\\":60,\\"min\\":null}]';$new=htmlspecialchars_decode($str);$new=str_replace('\\','',$new);$new1=json_decode($new,true);echo "
";print_r($new1);echo "
";

Array
(
[0] => Array
(
[Table] =>
[Field] => value
[Max] => 60
[Min] =>
)

)


This method has been tried, and there is nothing wrong, just if you
$s = '[{\\"table\\":\\"a\\",\\"field\\":\\"value\\",\\"max\\":60,\\"min\\":null}]';

Change
$s=$_POST['json'];

No.
The problem is that the POST data,
However, when JavaScript eval is used in HTML to convert the string into a json object, the line feed is normal.

Echo base64_encode ($ _ POST ['json']);
Post result

Base64_encode and then echo

W3tcXCZhbXA7cXVvdDt0YWJsZVxcJmFtcDtxdW90OzpcXCZhbXA7cXVvdDtlcHFcXCZhbXA7cXVvdDssXFwmYW1wO3F1b3Q7ZmllbGRcXCZhbXA7cXVvdDs6XFwmYW1wO3F1b3Q7c3RhbmQ0XFwmYW1wO3F1b3Q7LFxcJmFtcDtxdW90O21heFxcJmFtcDtxdW90Ozo2MCxcXCZhbXA7cXVvdDttaW5cXCZhbXA7cXVvdDs6bnVsbH1dW10=W10=

Echo base64_encode ($ _ POST ['json']);
Post result


I have read the base64 encoding and decoding. I need to use html_entity_decode twice. thank you, moderator. thank you, James.

What we can see in HTML is:

 

But when it comes to PHP, it becomes
[{\\"table\\":\\"epq\\",\\"field\\":\\"stand4\\",\\"max\\":60,\\"min\\":null}]

(Echo output to html is not visible & quot)
So html_entity_decode is required twice.

$s = 'W3tcXCZhbXA7cXVvdDt0YWJsZVxcJmFtcDtxdW90OzpcXCZhbXA7cXVvdDtlcHFcXCZhbXA7cXVvdDssXFwmYW1wO3F1b3Q7ZmllbGRcXCZhbXA7cXVvdDs6XFwmYW1wO3F1b3Q7c3RhbmQ0XFwmYW1wO3F1b3Q7LFxcJmFtcDtxdW90O21heFxcJmFtcDtxdW90Ozo2MCxcXCZhbXA7cXVvdDttaW5cXCZhbXA7cXVvdDs6bnVsbH1dW10=W10=';$s = base64_decode($s);$s = str_replace('\\', '', $s);$s = html_entity_decode($s);$s = html_entity_decode($s);echo $s, PHP_EOL;print_r(json_decode(substr($s, 0, -4), 1));
[{"table":"epq","field":"stand4","max":60,"min":null}][]?Array(    [0] => Array        (            [table] => epq            [field] => stand4            [max] => 60            [min] =>         ))
Let's see what's going on.
In fact, this general debugging should be mastered

Let's see what's going on.
In fact, this general debugging should be mastered


Thank you!
I haven't used PHP for a long time, and I have never learned the system. many of them haven't.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.