「Php/ツール/行末が\r\nであるファイルの検出」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「hoge」) |
|||
| 行1: | 行1: | ||
| − | + | <pre> | |
| + | <?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); | ||
| + | |||
| + | </pre> | ||
2025年3月7日 (金) 22:05時点における版
<?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);
