(note: i am not asking you to code this for me
counting line numbers in my project
Moderator: General Moderators
- mydimension
- Moderator
- Posts: 531
- Joined: Tue Apr 23, 2002 6:00 pm
- Location: Lowell, MA USA
- Contact:
counting line numbers in my project
has anyone tried doing something like this? i would like to have a "fun facts section of my site and on this page show the current number of lines of code in my project. couls someone point me in a direction to go with this? i would really appreciate it.
(note: i am not asking you to code this for me
)
(note: i am not asking you to code this for me
Try some thing like this
Only had a moment to test it, but it should return your selected file $file
In a textarea, enlightened by the length of the file.
Hope helped
davro
Only had a moment to test it, but it should return your selected file $file
In a textarea, enlightened by the length of the file.
Code: Select all
<?
$file = "links.php";
$contents = file($file);
$length = sizeof($contents);
for($i=0; $i<$length; $i++)
{
// buffers length, textarea for resizing
}
// display enlightened form for display
print "<p>File: $file has $length lines of text</p>\n";
print "<textarea border='0' name='text_area' wrap='off' rows='$i' cols='85'>\n";
for($j=0; $j<$length; $j++)
{
// show the file line by line, in the textarea.
print "$contentsї$j]";
}
// finish textarea
print "</textarea>\n";
?>davro
Last edited by davro on Fri Nov 01, 2002 7:01 am, edited 2 times in total.
Well. if the file was named .phps, or anything but .php then it is easy to do. A simple 'while !eof $fs' scheme will work fine. If it is named .php, and constantly make a renamed copy is just too much work, (i can sympathize with that) then I'm not sure. I would think that if you used PHP to access another PHP file, even just to read the number of lines, it might try to execute the code. I could be wrong, though, I've never tried.
that example has a LOT of overhead, and I don't recommend using it literally. I grabbed it from one of my corporate projects here. But the concept of it would work great for counting lines.
Code: Select all
<?php
$fd = fopen($bufferfile, "r");
while (!feof ($fd)) {
// count code
}
close($fd);
?>or simply do a:
Code: Select all
Currently there are: <b>
<?php
$file = ("yourfile.whatever");
$count = count($file);
print "$count";
?>
</b> lines in the project fileI'm glad you expounded on that, good point. As long as you can read a .php file via a php script without executing it, then yeah, its easy to count the ';'s I'm tempted to write the code when I get home and post it.jonsyd wrote:sorry to repeat myself, but why not count the ";" s?
Surely we want to count the number of LOC, not the number of lines in each file (unless you count blank lines, HTML, brackets and <?php / ?> as LOC)
- mydimension
- Moderator
- Posts: 531
- Joined: Tue Apr 23, 2002 6:00 pm
- Location: Lowell, MA USA
- Contact:
- mydimension
- Moderator
- Posts: 531
- Joined: Tue Apr 23, 2002 6:00 pm
- Location: Lowell, MA USA
- Contact:
for anyone interested, i have written a script which counts the number semi-colons and the number of lines containg either an opened brace or a closed brace BUT NOT both. here it is for your viewing pleasure.
(edit: i mod'ed the function so that all variables were declared inside the function)
note: wherever you see & replace with &
again i thank you all for your input. consider this your pay

(edit: i mod'ed the function so that all variables were declared inside the function)
Code: Select all
<?php
function countCodeLines ($directory) {
static $totalCodeLines;
$dir = opendir($directory);
while ($item = readdir($dir)) {
if ((is_dir($item)) && ($item != ".") && ($item != "..")) {
countCodeLines($item); //recursive directory walking
} elseif (strrchr($item, ".") == ".php") { //count only php files
$lines = file($directory . "/" . $item);
foreach ($lines as $line) {
//count lines containg a semi-colon or
//lines containing either an opened brace or a closed brace BUT NOT both
if (preg_match("/\;/", $line) xor (preg_match("/\{/", $line) xor preg_match("/\}/", $line))) {
$totalCodeLines++;
}
}
}
}
return $totalCodeLines; //return the final result
}
print countCodeLines($_SERVERї'DOCUMENT_ROOT']);
?>again i thank you all for your input. consider this your pay