[nycphp-talk] What does this code do?

John Campbell jcampbell1 at
Mon Mar 3 13:25:39 EST 2008

On Mon, Mar 3, 2008 at 1:12 PM, -- rada -- <radalapsker at> wrote:
> Hi all,
> I am trying to implement a user-input scrubbing function, more specifically,
> normalize linebreaks from windows/mac to unix. I can just do preg_replace
> but I saw this piece of code somewhere and don't understand it enough to
> even know what to google.
> supposedly converts Windows CRLF  to Unix LF:
> $str =~ s/\r\n/\n/g;
> What does =~ do? I can RTFM if you point me to it :)

That is perl code, and will not work in php.  The php equivalent is:

$str = preg_replace('/\r\n/',"\n",$str);

It means replace all carriage returns followed by a linefeed with just
a linefeed.

John Campbell

More information about the talk mailing list