LoginSignup
1
2

More than 5 years have passed since last update.

PHPでテキストファイルを扱う場合のtips

Last updated at Posted at 2016-03-12

PHPで標準入力とファイル指定両方扱う


if ($argc<2) {
    // 標準入力からデータを取得
    $file='php://stdin';
} else {
    // 最初のパラメータにファイルを指定した場合
    $file=$argv[1];
}
main($file);

function main($file)
{
    $fp=fopen($file,'r');
    if (! $fp) {
        echo "can't open ($file)\n";
        return;
    }
    while(($ln=fgets($fp))!==false) {
        $ln=chop($ln);
        proc($ln);
    }
}

複数の区切り文字を分割(multiexplode)

※ PHPドキュメントそのまま

function multiexplode ($delimiters,$string) {

    $ready = str_replace($delimiters, $delimiters[0], $string);
    $launch = explode($delimiters[0], $ready);
    return  $launch;
}
$val = multiexplode(array("=","[","]",",","(",")"), $line);
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2