KBD

Keith Devens .com

Friday, September 3, 2010 Flag waving
The status is *not* quo. – Dr. Horrible
← Jim's Inklog designLine count for code on my site →

Daily link icon Sunday, October 17, 2004

How to do a line count of all included files in PHP

For future reference, here's some quick-and-dirty code to get a line count of all included files during a PHP execution:

<?php
    $files 
get_included_files();
    
usort($files,create_function('$a,$b','return strcmp(basename($a),basename($b));'));
    
$t 0;
    foreach(
$files as $f){
        
$c count(file($f));
        
$t+=$c;
        echo 
'| '.basename($f).' | '.$c." |\n";
    }
    echo 
$t;
?>

(By the way, the reason I used the vertical bars in the output is because that's the markup for tables on my weblog. See the following post for some of the output.)

← Jim's Inklog designLine count for code on my site →

Comments XML gif

Joe Grossberg (http://www.joegrossberg.com) wrote:

Why would you want that information?

Also, shouldn't you exclude lines that don't contain any code, such as multi-line comments and blank lines?

∴ Joe Grossberg | 21-Oct-2004 11:12am est | http://www.joegrossberg.com | #5972

Keith (http://keithdevens.com/) wrote:

Why would you want that information?

I was just interested in seeing how much code I'm including on each page execution.

Also, shouldn't you...

Well... that's why I called it "quick and dirty". Smiley I could strip out code-less lines, but even after that you're always going to have some variation - some people's code is more dense than others, even coming down to simple variations such as:

if(foo)
{
}
else
{
}

vs.

if(foo){
}else{
}

though also including other aspects of code style. In any case, it's probably really easy to strip code-less lines with some quick regular expressions without making it much less quick or dirty, so I may do that if I need this again in the future.

Keith | 22-Oct-2004 5:58pm est | http://keithdevens.com/ | #6006

Jay (http://hidemein.com) wrote:

Hi there Keith I'm a bit of a novice with PHP and I'm tiring to script a code to analyze the number of characters in the source of a html document. The trouble is there are no (only at the top) <some html statement> present as the page is ASCII text in both the source and browser zones.

I'm tiring to set the script to count chars from predesignated lines eg: count "some ASCII char" from (line 62 col 32) to (line 62 first space)as an example.

Question: Are you fetching <> statements form html tags for the code above and if (or if not) how does one retrieve the character count without the fetch <some html statement> but with a line (y) col (x) coordinate statement instead?.

∴ Jay | 5-Nov-2009 8:12am est | http://hidemein.com | #11440

Jay (http://hidemein.com) wrote:

Keith @ http://keithdevens.com
Hi there again I searched around for a starting point and found the following. It's at least solved the (line 62 first space) problem as it only count the characters in the string and not it's spaces.

As you notice it can handle in Unicode fonts which is a bonus - echo unichar("áá b"); and echo unichar("as ñ"); although the line count in the document is standard ASCII. I've also deliberately placed spaces in the strings to test if it would nullify these in the results, here's where I'm at:
________________________________
<?php
function unichar($string) {
$two= strtolower(str_replace(' ', '', $string));
$res = count(count_chars($two, 1));
return $res;
}

echo unichar("áá b");
print (',');
echo unichar("as ñ");
print (',');
echo unichar("adddd b");
print (',');
echo unichar("sssa b");
print (',');
echo unichar("as b");
print (',');
echo unichar("asewewqw b");
print (',');
echo unichar("astrrtrtewtwt b");
print (',');
echo unichar("asr b");
print (',');
echo unichar("wrtwt wrtwtw wrtwrt rtw rt w as b");
print (',');
echo unichar("abc ");

?>

_______________________________
outputs:
2,3,3,3,3,6,7,4,6,3
_______________________________

What I think I need to do is somehow place ($ ??) into the strings to call for the specific lines from the page source of the document in question, and I'm guessing with the CURL function or similar retrieve (fetch) method. As I stated before I'm only a novice but I've got the basic gist of PHP scripting.

Any advice or pointers in the right direction would be most grateful, cheers!

PS: this is a script for personal use (non profit) and is engineering, analytical based for the purpose of automating - calculating information dynamics form a text/html document.

∴ Jay | 5-Nov-2009 12:27pm est | http://hidemein.com | #11441

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

September 2010
SunMonTueWedThuFriSat
 1234
567891011
12131415161718
19202122232425
2627282930 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 2 posts

Recent comments XML

new⇒Call a function from a string in Python

or​use:
?!code:language=python
def​fce(arg1=None,arg2=None):
#some​usefu...

Richard: Sep 2, 1:08pm

new⇒Spider solitaire

Been playing 4S Spider for a couple​of years. Only recently did I start​to ...

jimibd: Sep 2, 3:16am

Generated in about 0.159s.

(Used 8 db queries)