Re: DOS newlines (CR/LF) to Unix format [message #171731] |
Wed, 19 January 2011 11:24 |
Kim Andr Aker
Messages: 17 Registered: September 2010
Karma:
|
Junior Member |
|
|
På Wed, 19 Jan 2011 11:59:48 +0100, skrev Bjarne Jensen
<bjarne(dot)b(dot)jensen(at)gmail(dot)com>:
> I found this snippet on internet:
>
> # IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format
> awk '{sub(/\r$/,"");print}' # assumes EACH line ends with Ctrl-M
>
> It works fine on the commandline so I wrote like this in a php-script:
>
> - - -
>
> $unix = ".unix";
>
> foreach (glob($usedir."[12_]*") as $filename) {
> exec('awk { sub("/\r$", ""); print } $filename > $filename.$unix');
> }
>
> - - -
>
>
> But absolutely nothing happens!
>
> Why not?
It's probably either one of two reasons, or a combination of the two.
One, your glob() function may not return any items.
Two, your system may have disabled the use of the exec(), passthru()
and/or system() functions due to security. In any case, running a command
for this in PHP is not needed, just use the built-in PHP functions instead.
Assuming you use PHP 5, and that your use of the glob() function is
correct:
$unix = ".unix";
foreach (glob($usedir."[12_]*") as $filename) {
// doing this in a single operation
file_put_contents($filename.$unix, str_replace("\r", "",
file_get_contents($filename)));
}
--
Kim André Akerø
- kimandre(at)NOSPAMbetadome(dot)com
(remove NOSPAM to contact me directly)
|
|
|