PHP计算文件的行数和行数(每行的长度)

时间:2017-04-03 11:49:31

标签: php

我有一个像这样的php文件。

Line   |   Length of Line
              1 2 3 4 5 6 7 8 9
1        |  < ? php
2        |
3        |                $g r a d e = 5;
4        |
5        |  ? >

如何计算php中此文件中每行的总行数和长度?

1 个答案:

答案 0 :(得分:0)

这个堆栈部分回答了您的问题:https://stackoverflow.com/a/13246630/6535806,只需要在一段时间内获得 $line 的长度。因此代码应如下所示:

$handle = fopen("filename.extention", "r");
// if the file exist
if ($handle) {
    // for each line
    while (($line = fgets($handle)) !== false) {
        // write the length of the line
        echo strlen($line).'<br />';
    }
    // then we close the file opened
    fclose($handle);
} else {
    // error opening the file.
}
相关问题