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 118139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:26:51+00:00 2026-05-11T03:26:51+00:00

What’s the best way to remove comments from a PHP file? I want to

  • 0

What’s the best way to remove comments from a PHP file?

I want to do something similar to strip-whitespace() – but it shouldn’t remove the line breaks as well.

For example,

I want this:

<?PHP // something if ($whatsit) {     do_something(); # we do something here     echo '<html>Some embedded HTML</html>'; } /* another long comment */ some_more_code(); ?> 

to become:

<?PHP if ($whatsit) {     do_something();     echo '<html>Some embedded HTML</html>'; } some_more_code(); ?> 

(Although if the empty lines remain where comments are removed, that wouldn’t be OK.)

It may not be possible, because of the requirement to preserve embedded HTML – that’s what’s tripped up the things that have come up on Google.

  • 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. 2026-05-11T03:26:51+00:00Added an answer on May 11, 2026 at 3:26 am

    I’d use tokenizer. Here’s my solution. It should work on both PHP 4 and 5:

    $fileStr = file_get_contents('path/to/file'); $newStr  = '';  $commentTokens = array(T_COMMENT);      if (defined('T_DOC_COMMENT')) {     $commentTokens[] = T_DOC_COMMENT; // PHP 5 }  if (defined('T_ML_COMMENT')) {     $commentTokens[] = T_ML_COMMENT;  // PHP 4 }  $tokens = token_get_all($fileStr);  foreach ($tokens as $token) {         if (is_array($token)) {         if (in_array($token[0], $commentTokens)) {             continue;         }                  $token = $token[1];     }      $newStr .= $token; }  echo $newStr; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing a source code file, and I want to remove all line comments
what's the best way to remove a row (QTreeWidgetItem) from a QTreeWidget? The QTreeWidget
what would be the best way to remove whitespace only around certain character. Let's
Good morning ! What is the best way to remove duplicate records from grid
What's the best way to remove these tags from a string, to prepare it
We need a way to remove HTML comments when displayed to the user but
The best way to remove duplicate values ( NSString ) from NSMutableArray in Objective-C?
What is the best way to remove all the special characters from a string
What is the best way to remove duplicate entries from a Data Table?
What is the best way to remove all merge fields from a word 2010

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.