facebook twitter hatena line email

「Php/ツール/行末が\r\nであるファイルの検出」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「hoge」)
 
(Admin がページ「Php/ツール/行末が¥r¥nであるファイルの検出」を「Php/ツール/行末が\r\nであるファイルの検出」に、リダイレクトを残さずに移動しました)
 
(同じ利用者による、間の1版が非表示)
行1: 行1:
hoge
+
<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月8日 (土) 00: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);