note 87816 modified in function.preg-replace by danbrown
| From: | danbrown@php.net | Date: | Fri, 26 Dec 2008 17:04:31 +0000 |
| Subject: | note 87816 modified in function.preg-replace by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-148546@lists.php.net to get a copy of this message | ||
<?php
/*
Coding across linux, mac, and windows gets annoying dealing with errors resulting from EOL format in
config files, etc.
Standardize to CRLF format.
Instead of using one regex I just broke this up into smaller groups -
may take longer to execute three statements rather than one,
but it also granularizes the use case.
*/
//different formats all living together
$s = "Testing 1.\r" . "Testing 2.\n" . "Testing 3.\r\n" .
"Testing 4.\n\r:END";
$s = preg_replace("/(?<!\\n)\\r+(?!\\n)/", " :REPLACED: \r\n", $s); //replace
just CR with CRLF
$s = preg_replace("/(?<!\\r)\\n+(?!\\r)/", " :REPLACED: \r\n", $s); //replace
just LF with CRLF
$s = preg_replace("/(?<!\\r)\\n\\r+(?!\\n)/", " :REPLACED: \r\n", $s);
//replace misordered LFCR with CRLF
echo $s;
/*
output:
Testing 1. :REPLACED:
Testing 2. :REPLACED:
Testing 3.
Testing 4. :REPLACED:
:END
*/
?>
--was--
/*
Coding across linux, mac, and windows gets annoying dealing with errors resulting from EOL format in
config files, etc.
Standardize to CRLF format.
Instead of using one regex I just broke this up into smaller groups -
may take longer to execute three statements rather than one,
but it also granularizes the use case.
*/
//different formats all living together
$s = "Testing 1.\r" . "Testing 2.\n" . "Testing 3.\r\n" .
"Testing 4.\n\r:END";
$s = preg_replace("/(?<!\\n)\\r+(?!\\n)/", " :REPLACED: \r\n", $s); //replace
just CR with CRLF
$s = preg_replace("/(?<!\\r)\\n+(?!\\r)/", " :REPLACED: \r\n", $s); //replace
just LF with CRLF
$s = preg_replace("/(?<!\\r)\\n\\r+(?!\\n)/", " :REPLACED: \r\n", $s);
//replace misordered LFCR with CRLF
echo $s;
/*
output:
Testing 1. :REPLACED:
Testing 2. :REPLACED:
Testing 3.
Testing 4. :REPLACED:
:END
*/
http://php.net/manual/en/function.preg-replace.php