PHP array Conversion String function, I use thinkphp, Json_encode save Chinese, backslash disappeared

Source: Internet
Author: User
PHP array Conversion String function, I use thinkphp, Json_encode save Chinese, backslash disappeared
This is the original.
[{"title": "\u5173\u6ce8\u65f6\u56de\u590d", "url": "\/index.php?g=user****"}]
Turns out this is it.
[{"title": "u5173u6ce8u65f6u56deu590d", "url": "\/index.php?g=user****"}]
Is Chinese, with json_decode processing, save to the database there is no backslash,

PHP version 5.2, Apache 2.2

I remember having an array conversion string that could hold the variable type, forgetting what to call


Reply to discussion (solution)

It's a loose split.
Serialize ($array);
Unserialize ($array);
There was a surprise, found

Is it thinkphp's problem?
The Json_encode directly with PHP will not have a backslash.

Echo '
 
   ; $arr = Array (        ' title ' + ' reply ',        ' url ' = '/index.php?g=user**** '    )); $str = Json_encode ($arr); echo $str. '
'; Echo '
';p Rint_r (Json_decode ($str, True)); Echo '
';


[{"title": "\u5173\u6ce8\u65f6\u56de\u590d", "url": "\/index.php?g=user****"}]
Array
(
[0] = = Array
(
[title] = reply when you are concerned
[url] =/index.php?g=user****
)

)

Should be the cause of the environment, the slash is disposed of.

Do you see any processing that stripslashes the data, before saving it to the database

The next point of congratulations landlord

Do you see any processing that stripslashes the data, before saving it to the database



It's not clear, it should be handled,
Where would you like to set up?
I use Json_decode with a backslash, save to the database there is no,
Also local OK, upload the server is not, configure the same
Local PHP 5.5
Server PHP 5.2,
Using the Serialize function instead,
return string, array variable type will be saved, unchanged

Is it thinkphp's problem?
The Json_encode directly with PHP will not have a backslash.

Echo '
 
   ; $arr = Array (        ' title ' + ' reply ',        ' url ' = '/index.php?g=user**** '    )); $str = Json_encode ($arr); echo $str. '
'; Echo '
';p Rint_r (Json_decode ($str, True)); Echo '
';


[{"title": "\u5173\u6ce8\u65f6\u56de\u590d", "url": "\/index.php?g=user****"}]
Array
(
[0] = = Array
(
[title] = reply when you are concerned
[url] =/index.php?g=user****
)

)


The feeling is the thinkphp problem, after the preservation has not, $db->save (JSON);

For sub-use of joints

Save is saved to the database, then your "\u5173\u6ce8\u65f6\u56de\u590d" in the \ do not need to escape it? TP will do it for you?

Save is saved to the database, then your "\u5173\u6ce8\u65f6\u56de\u590d" in the \ do not need to escape it? TP will do it for you?



You know, you need this, right?
Str_replace ("/\\/", "\\\\", JSON)
Is it necessary to escape writing to the database?

Yes, to escape special characters when writing to the database

Yes, to escape special characters when writing to the database


Well, thank you, previously used ASP, only the value of the variable inside the tube, you can write to the database, know
Thanks for the knot.
, Json_encode (Array ())  ;//write to database backslash disappears, need to escape

ASP \ is not a special character, but ' still to be escaped '

  • Related Article

    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.