Solved

PHP Function to Remove Comments

Posted on 2008-10-24
6
1,688 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

747 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

7 Experts available now in Live!

Get 1:1 Help Now