Using PHP and MySQL. I need to do something similar to how Github does when it shows source code,
122 lines (98 sloc) 4.003 kb
I need to get the total number of Lines Of Code and Source Lines Of Code from a MySQL database result.
The source code will be in a wordpress table as a meta field, I know how to accomplish this with a file, but not with a Database result. I then need to also calculate the disk space used on this field to show something like 4.003 kb
If you know how to do any of this, I would appreciate any help
UPDATE
This very ugly code is the only solution I have found so far, if you search SO or Google, all the results show how to count lines of code on a FILE, my problem is very different, I am not fetching a file, I have a result from a MySQL database held in a $variable
This code below does 1 of the 3 things, it ets total number of lines of (LOC) so I would just need to get (SLOC) and (Disk space used) however I am very open to a better way of counting the LOC of a variable.
// Get LOC
$a = $code_source; // Variable holding our code
$result = count_chars($a, 0);
for ($i=0; $i < count($result); $i++) {
if ($result[$i] != 0) {
if (chr($i) == "\n") // line feed
$n = $result[$i];
if (chr($i) == "\r") // carriage return
$r = $result[$i];
}
}
if ($n > $r) $l = $n + 1;
if ($r >= $n) $l = $r + 1;
if (!isset($l) ) $l = "2";
echo "Line Of Code = " . $l;
Space
//Get Disk Space Used
if (function_exists('mb_strlen')) {
$size = mb_strlen($code_source, '8bit');
} else {
$size = strlen($code_source);
}
if($size >= 1024)
$size = round($size / 1024, 2).' KB';
else
$size = $size.' bytes';
echo 'size of file' . ': ' . $size;
2nd Update
Ok here is the end result I have now, I omitted @refp’s (LOC) code as I could not get it to work for some reason, I did use his SLOC code and put in all into a usable class. Please fill free to improve
class SourceCodeHelper{
// Count total number of Lines (LOC)
public function countLOC($string){
$result = count_chars($string, 0);
for ($i=0; $i < count($result); $i++) {
if ($result[$i] != 0) {
if (chr($i) == "\n\n") // line feed
$n = $result[$i];
if (chr($i) == "\r") // carriage return
$r = $result[$i];
}
}
if ($n > $r) $l = $n + 1;
if ($r >= $n) $l = $r + 1;
if (!isset($l) ) $l = "2";
//substr_count ($data, "\n") + 1;
return $l;
}
// Count total Source code lines of Code (SLOC)
public function countSLOC($string){
return count(preg_split("/\n\s*/", $string));
}
// Calculate disk space usage of string
public function stringDiskSpace($string){
if (function_exists('mb_strlen')) {
$size = mb_strlen($string, '8bit');
} else {
$size = strlen($string);
}
return diskSpacePretty($size);
}
// Format a disk space usage into human readable format
public function diskSpacePretty($size){
if($size >= 1024)
$size = round($size / 1024, 2).' KB';
else
$size = $size.' bytes';
return $size;
}
}
There is no straight forward way of counting the number of lines in a field from the inside of mysql.
Instead I recommend you to append a new column to your table where you store the number of lines. Calculate the LOC/SLOC by using PHP (or whatever backend language is responsible for inserting data) and update it when neccessary.
To count the number of lines (LOC) in a string you can use substr_count which will count the number of occurrences of the second parameter string inside of the first one.
In the below example we will count how many times
\nis present inside$data.To count the number of SLOC you can use preg_split to filter off all empty lines (lines of length 0 or only containing white spaces)