Re: DOS newlines (CR/LF) to Unix format [message #171904 is a reply to message #171731] |
Thu, 20 January 2011 18:58 |
Bjarne Jensen
Messages: 9 Registered: January 2011
Karma:
|
Junior Member |
|
|
Hej Kim,
Mange tak - det funker!
Mvh Bjarne
- - -
On 2011-01-19 12:24, Kim André Akerø wrote:
> 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)));
> }
>
>
|
|
|