Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6932287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:41:13+00:00 2026-05-27T11:41:13+00:00

Using PHP and MySQL. I need to do something similar to how Github does

  • 0

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;
    }

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:41:13+00:00Added an answer on May 27, 2026 at 11:41 am

    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 \n is present inside $data.

    substr_count ($data, "\n") + 1; // since one \n creates two lines
    

    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)

    count (preg_split ("/\n\s*/", $data)) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently using PHP/MySQL and the jQuery timeago plugin. Basically, I need to
I am developing a PHP/MySQL application using vertrigoserver. I need to enter the German
I have a product catalog using apache, php and mysql. I need to put
I need a method of stopping concurrent logins using PHP/MySQL. The current setup Currently
I'm using mysql/php/apache . I have the following situation: 2 tables where I need
Using PHP / MySQL all encoded up as UTF, we have recently had to
Been using PHP/MySQL for a little while now, and I'm wondering if there are
I'm using PHP/MySql. If I have two tables (hypothetically) as follows: id name and
i have created a forum using php/mysql and users login through facebook using php
Im trying to create a iPhone Objective C login page using PHP/MySQL to authenticate.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.