how to remove new lines and returns from php string?

后端 未结 10 1119
孤城傲影
孤城傲影 2020-12-24 01:06

A php variable contains the following string:

text

text2

  • item1
  • item2
10条回答
  •  清酒与你
    2020-12-24 01:49

    Correct output:

    '{"data":[{"id":"1","reason":"hello\\nworld"},{"id":"2","reason":"it\\nworks"}]}'
    
    function json_entities( $data = null )
    {           
        //stripslashes
        return str_replace( '\n',"\\"."\\n",
            htmlentities(
                utf8_encode( json_encode( $data)  ) , 
                ENT_QUOTES | ENT_IGNORE, 'UTF-8' 
            )
        );
    }
    

提交回复
热议问题