Escaping escape Characters

前端 未结 6 1301
悲哀的现实
悲哀的现实 2020-12-31 17:41

I\'m trying to mimic the json_encode bitmask flags implemented in PHP 5.3.0, here is the string I have:

$s = addslashes(\'O\\\'Rei\"lly\'); // O         


        
6条回答
  •  清歌不尽
    2020-12-31 18:15

    If I understand correctly, you just want to know why you need to use

    '\\\u0027' and not just '\\u0027'

    You're escaping the slash and the character unicode value. With this you are telling json that it should put an apostrophe there, but it needs the backslash and the u to know that a unicode hexadecimal character code is next.

    Since you are escaping this string:

    $s = addslashes('O\'Rei"lly'); // O\'Rei\"lly
    

    the first backslash is actually escaping the backslash before the apostrophe. Then next slash is used to escape the backslash used by json to identify the character as a unicode character.

    If you were appplying the algorythm to O'Reilly instead of O\'Rei\"lly then the latter would suffice.

    I hope you find this useful. I only leave you this link so you can read more on how json is constructed, since its obvious you already understand PHP:

    http://www.json.org/fatfree.html

提交回复
热议问题