PHP replacing literal \r\n with <br/> (not replacing new lines)

You could try this:

$body = nl2br(strtr($post[1], array('\r' => chr(13), '\n' => chr(10))));

$body = isset($post[1]) ? preg_replace('#(\\\r|\\\r\\\n|\\\n)#', '<br/>', $post[1]) : false;

You'll need three \\\. Inside single quotes, \\ translates to \ so \\\r becomes \\r which gets fed to the preg_replace funciton.

PREG engine has its own set of escape sequences and \r is one of them which means ASCII character #13. To tell PREG engine to search for the literal \r, you need to pass the string \\r which needs to be escaped once more since you have it inside single quotes.


If it's displaying \r and \n in your html, that means that these are not newlines and line breaks, but escaped backslashes followed by an r or an n (\\r for example). You need to strip these slashes or update your regex to account for them.