How to replace/escape U+2028 or U+2029 characters in PHP to stop my JSONP API breaking

前端 未结 2 1039
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-01 06:15

Ok I am running a public JSONP API which the data is served from my PHP server. I just read this article:

  • JSON: The JavaScript subset that isn\'t (by Magnus Holm;
2条回答
  •  佛祖请我去吃肉
    2021-02-01 06:51

    You can replace U+2028, U+2029 with "\u2028", "\u2029" either on the PHP side or the JavaScript side, or both, it doesn't matter as long as it happens at least once (it's idempotent).

    You can just use ordinary string replacement functions. They don't need to be "multibyte safe", and you can do it just as easily in any Unicode encoding (UTF-8, UTF-16, UTF-32 are all equally fine). PHP didn't have Unicode escape sequences last time I checked which is just one more reason why PHP is a joke but you can use the \x escape with UTF-8...

    (In short, the reason there's no multibyte string replace function is because it would be redundant -- it would be exactly the same as a non-multibyte string replace function.)

    // Javascript
    data = data.replace("\u2028", "\\u2028").replace("\u2029", "\\u2029");
    
    // PHP
    $data = str_replace("\xe2\x80\xa8", '\\u2028', $data);
    $data = str_replace("\xe2\x80\xa9", '\\u2029', $data);
    

    Or you could just do nothing at all, since PHP escapes non-Unicode characters by default in json_encode():

    // Safe
    echo json_encode("\xe2\x80\xa9");
    --> "\u2029"
    
    // Correct JSON, but invalid Javascript...
    // (Well, technically, JSON root must be array or object)
    echo json_encode("\xe2\x80\xa9", JSON_UNESCAPED_UNICODE);
    --> "
    "
    

提交回复
热议问题