?
Solved

PHP Function to Remove Comments

Posted on 2008-10-24
6
Medium Priority
?
1,700 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 13

Assisted Solution

by:AielloJ
AielloJ earned 400 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 1600 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
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…
Suggested Courses

741 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