[PHP-DEV] Bug #485 Updated: ereg_replace and Linefeeds From: Bug Database (php-dev <email protected>)
Date: 06/27/98

ID: 485
Updated by: rasmus
Reported By: philip <email protected>
Status: Closed
Bug Type: Misbehaving function
Assigned To:
Comments:

Looks like expected behaviour to me. When you put an escape sequence inside a quoted string in PHP it gets replaced by its corresponding value before getting sent to the function you are using it in. That means you have to double-escape escape sequences for regex functions for which escape sequences also have meaning. A bit confusing, but the only real way to stay consistent. So, if you want to escape all newlines simply do this:
$s2 = ereg_replace("\n","\\\n",$s1);
That looks for a newline and replaces it with a backslash (which you of course have to escape) followed by a newline. So, \\ to represent the backslash and \n to represent the newline.

Full Bug description available at: http://ca.php.net/bugs.php3?id=485