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

  • SEARCH
  • Home
  • 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 6772085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:30:27+00:00 2026-05-26T15:30:27+00:00

I am writing a PHP program which reads file with file_get_contents then attempts to

  • 0

I am writing a PHP program which reads file with file_get_contents then attempts to count effective lines in that source file. It must not count empty lines or lines containing comments only. Sample file:

<?php

/**
  * blah blah
  */
class Test {
     // testfunc
     function testfunc(){


         return;
     }
}

The number of lines in such a file should be 5. Here is what I’ve got so far:

    $f=file_get_contents($this->file);
    $f=preg_replace('|/\*.*?\*/|s','',$f);
    $f=preg_replace('/^\s*$/','',$f);  // <-- does not work
    $f=preg_replace("/\n\n*/s","\n",$f);
    $count=count(explode("\n",$f));

But for some reason it does not eliminate white-spaces. Is there a better way to get this done?


The following code does the job, since I don’t care much about the spaces, but I still wonder, why my original line labeled “does not work” is not removing spaces from empty lines. Is there some extra character at the end? File format is unix.

    $f=preg_replace('/ */','',$f);   // removes all spaces properly.
  • 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-26T15:30:28+00:00Added an answer on May 26, 2026 at 3:30 pm

    Change /^[\s\t]*$/ to be /^\s*$/ms and that should fix it.
    The \s class includes tabs, so no need to add \t. The s makes it match newline characters and the m option makes ^ and $ work when data contains multiple lines (matches line breaks).

    Also, it might be better to change /\n\n/s to be /[\r\n]{2,}/.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a program that must communicate with a PHP / web based
I am writing a php program to write a binary file (may be video
I'm a php programmer, i want writing a program that when i give to
I'm writing a PHP script and the script outputs a simple text file log
The Python program I'm writing needs to start a local PHP script outside of
I am writing a php app that interfaces with a companies SOAP API. I
I'm writing a quiz program using PHP and a bit of Javascript. The questions
I'm writing a program to find the file size of files. Is it possible
I'm writing a program, that will do some transformations with image from a webcam
I am writing a PHP/MySQL program and I would like to know how to

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.