Replacing \r\n (newline characters) after running json_encode Replacing \r\n (newline characters) after running json_encode mysql mysql

Replacing \r\n (newline characters) after running json_encode


Does the string contain "\r\n" (as in 0x0D 0x0A) or the literal string '\r\n'? If it's the former, this should remove any newlines.

$json = preg_replace("!\r?\n!", "", $json);

Optionally, replace the second parameter "" with "<br />" if you'd like to replace the newlines with a br tag. For the latter case, try the following:

$json = preg_replace('!\\r?\\n!', "", $json);


Don't replace it in the JSON, replace it in the source before you encode it.


I had a similar issue, i used:

$p_num = trim($this->recp);$p_num = str_replace("\n", "", $p_num);$p_num = str_replace("\r", ",", $p_num);$p_num = str_replace("\n",',', $p_num);$p_num = rtrim($p_num, "\x00..\x1F");

Not sure if this will help with your requirements.