PHP's json_encode does not escape all JSON control characters

后端 未结 12 1033
旧时难觅i
旧时难觅i 2020-11-28 12:08

Is there any reasons why PHP\'s json_encode function does not escape all JSON control characters in a string?

For example let\'s take a string which spans two rows a

12条回答
  •  星月不相逢
    2020-11-28 12:33

    Control characters have no special meaning in HTML except for new line in textarea.value . JSON_encode on PHP > 5.2 will do it like you expected.

    If you just want to show text you don't need to go after JSON. JSON is for arrays and objects in JavaScript (and indexed and associative array for PHP).

    If you need a line feed for the texarea-tag:

    $s=preg_replace('/\r */','',$s);
    echo preg_replace('/ *\n */','
',$s);
    

提交回复
热议问题