Solved

PHP Function to Remove Comments

Posted on 2008-10-24
6
1,689 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

929 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now