天天看點

php dos2unix

* LineProcessor.php

<?php
 
class LineProcessor {
    /** @var \SplFileObject */
    private $file;
 
    /** @var int */
    private $linum;
 
    /** @var  callable */
    protected $handler;
 
    public function __construct(string $path) {
        $this->file = new \SplFileObject($path, 'r');
        $this->linum = 0;
    }
 
    public function getline() /* :string */ {
        return $this->file->fgets();
    }
 
    /**
     * 試着讀取$limit行, 對每一行執行$callback
     * @param callable $callback
     * @param int $limit default unlimited
     * @return int 實際讀取的行數
     */
    public function forEach(callable $callback, int $limit = 0) {
        // unlimited
        if (0===$limit) {
            while ($this->file->valid()) {
                $line = $this->file->fgets();
                call_user_func($callback, $line, $this->linum);
                $this->linum++;
            }
            return 0;
        }
        for ($i = 0; $i < $limit && $this->file->valid(); $this->linum++) {
            $line = $this->file->fgets();
            if (!empty($line)) {
                call_user_func($callback, $line, $i);
                $i++;
            }
        }
        return $i;
    }
 
    public function setHandler(callable $cb) {
        if (!$cb) {
            $cb = function($cur, $i) {
                printf("%d %s\n", $i, $cur);
            };
        }
        $this->handler = $cb;
    }
 
    public function run() {
        $this->forEach($this->handler);
    }
 
}
           

* dos2unix.php

<?php
// usage: php7 dos2unix.php time.txt > time.unix.txt
function __autoload($className) {
    include $className.'.php';
}

$p = new LineProcessor($argv[1]);
$p->setHandler(function($cur) {
	$cur = preg_replace('/\r\n/', "\n", $cur);
    echo $cur;
});
 
$p->run();
           

* test:

time.txt

Usage: php7 dos2unix.php time.txt > time.unix.txt

時間	
9:00	12:00
18:00	22:00
18:00	22:00
18:00	22:00
9:00	12:00
9:00	12:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
9:00	12:00
9:00	12:00
18:00	22:00
18:00	22:00
18:00	22:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
	
	
	
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
9:00	12:00
18:00	22:00
18:00	22:00
9:00	12:00
9:00	12:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
9:00	12:00
9:00	12:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
9:00	12:00
9:00	12:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
9:00	12:00
9:00	12:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
9:00	12:00
9:00	12:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
9:00	12:00
9:00	12:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
9:00	12:00
9:00	12:00
9:00	12:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
9:00	12:00
9:00	12:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
9:00	12:00
9:00	12:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
9:00	12:00
9:00	12:00
9:00	12:00
18:00	22:00
18:00	22:00
9:00	12:00
9:00	12:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
9:00	12:00
9:00	12:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
9:00	12:00
9:00	12:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
9:00	12:00
9:00	12:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
9:00	12:00
9:00	12:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
9:00	12:00
9:00	12:00
9:00	12:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
9:00	12:00
9:00	12:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
9:00	12:00
9:00	12:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
18:00	22:00
9:00	12:00
9:00	12:00           
上一篇: 記事本
下一篇: dos2unix替代