Solved

PHP Function to Remove Comments

Posted on 2008-10-24
6
1,696 Views
Last Modified: 2012-06-21
I want to remove all comments from my script.  Everything else should remain in tact.
<?php
 
/**
 * Blah blah blah
 *
 * Text stuff
 *
 */
 
// Hello World
echo "<h1>Hello World</h1>";
 
/////////// Good-bye
echo "<h2>Good-bye</h2>";
 
#
#  The End
#
 
?>

Open in new window

0
Comment
Question by:hankknight
  • 3
  • 2
6 Comments
 
LVL 13

Expert Comment

by:AielloJ
ID: 22798014
Why would you want to remove the comments?  You PHP runs on your server and should not be readable by users.  There are class modules available that will remove comments is PHP files.

  Google: PHP remove comments

there are several listed.
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 22800747
hankknight,

How are you intending to do this?  Another PHP script, some program, etc?  You could find the code block with one or more regular expressions but then what runs the expression?  How you hope to do this will make a difference in your options.

Let me know if you have any questions or need more information.

b0lsc0tt
0
 
LVL 16

Author Comment

by:hankknight
ID: 22824545
I want one PHIP script that will remove comments from another PHIP script.  My intent is to decrease file size and increase performance of my scripts when I move them to my production server.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 13

Assisted Solution

by:AielloJ
AielloJ earned 100 total points
ID: 22825415
hankknight:

Unless there are huge amounts of comments in your code, the slight decrease in the file size isn't going to affect the performance.  PHP is an interpreted scripting language.  The interpretation by the PHP scripting engine will make any performance gains from removing comments negligable.

Are you currently running into any performance bottlenecks?
0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 400 total points
ID: 22825728
It isn't hard to do but the benefit may not be worth even the effort.  The line below ...
$result = preg_replace('%/\*[\s\S]+?\*/|(?://|#).*(?:\r\n|\n)%m', '', $sText);
... will replace the comments with an empty string, basically deleting the matches.  It should match any of the styles of comments you provided.  Depending on the text though there may be issues.  You should carefully test this.  If there is an issue then provide that text or a sample of it.
Let me know if you have a question about using this.
bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 22833651
I'm glad I could help.  Thanks for the grade, the points and the fun question.
bol
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Cpanel file manager 8 42
What does != "" mean in programming 8 84
PHP delete contents of file- before writing to it 6 26
Php logic to add to date card 9 24
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question