Php/ツール/行末が\r\nであるファイルの検出
提供: 初心者エンジニアの簡易メモ
<?php function detectCRLF($dir) { $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)); $crlfFiles = []; foreach ($rii as $file) { if ($file->isDir()) { continue; } if (pathinfo($file, PATHINFO_EXTENSION) === 'php') { $content = file_get_contents($file->getPathname()); if (preg_match("/\r\n/", $content)) { $crlfFiles[] = $file->getPathname(); } } } if (count($crlfFiles) > 0) { echo "CRLF detected in the following PHP files:\n"; foreach ($crlfFiles as $file) { echo $file . "\n"; } } else { echo "No PHP files with CRLF line endings found."; } } // 実行 $directory = $argv[1] ?? '.'; // コマンドライン引数が指定されていなければカレントディレクトリ detectCRLF($directory);