-2

svn追跡システムのコードを書いています。開発者が書いたコメントの数を数えたいです。

2文字間の行数を取得するphp関数はありますか?/*と*/の間の行数を取得したい

前もって感謝します。

4

3 に答える 3

2

Tokenizerを使用してPHPソースファイルを解析し、コメントをカウントできます。

$source = file_get_contents('source.php');
$tokens = token_get_all($source);
$comments = array_filter($tokens, function($token) {
    return $token[0] === T_COMMENT;
});

echo "Number of comments: " . count($comments);

これはコメントの数をカウントすることに注意してください。行数をカウントするには、改行$token[1](実際のコメント)をさらにカウントする必要があります。

アップデート

私はそれを試してみたかった、ここに行く:

$source = <<<PHP
<?php
/*
 * comment 1
 */
function f() {
  echo 'hello'; // comment 2
  // comment 3
  echo 'hello'; /* OK, this counts as */ /* three lines of comments */ // because there are three comments
}
PHP;

$tokens = token_get_all($source);
$comments = array_filter($tokens, function($token) {
    return $token[0] === T_COMMENT;
});
$lines = array_reduce($comments, function(&$result, $item) {
    return $result += count(explode("\n", trim($item[1])));
}, 0);

echo "Number of comments: ", count($comments), "\n";
echo "Lines of comments: ", $lines;

出力

Number of comments: 6
Lines of comments: 8

オンラインデモ

于 2013-03-12T13:12:25.777 に答える
0

preg_replaceタグ間のすべてを削除してから/* */、行を数えるために使用できます。

<?php
$string = <<<END
just a test with multiple line

/*
some comments

test
*/

and some more lines
END;

$lines = explode(chr(10), $string);
echo 'line count: ' . (count($lines)+1) . '<br>';
//line count: 10

$pattern = '/\/\*(.*)\*\//s';
$replacement = '';
$string = preg_replace($pattern, $replacement, $string);


$lines = explode(chr(10), $string);
echo 'line count: ' . (count($lines)+1);
//line count: 6
?>
于 2013-03-12T11:38:23.947 に答える
0

開始点として、 PHPリフレクションライブラリgetDocComment()を使用してみることができますが、インラインコメントはおそらくフェッチされません。

于 2013-03-12T11:38:47.640 に答える