note 87816 deleted from function.preg-replace by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 06:47:47 +0000 |
| Subject: | note 87816 deleted from function.preg-replace by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-196647@lists.php.net to get a copy of this message | ||
Note Submitter: montana [at] percepticon [dot] com
----
<?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
*/
?>