Solved

PHP Function to Remove Comments

Posted on 2008-10-24
6
1,697 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
A responsive image gallery using flexbox 6 76
How do I speed up this PDO query 4 40
e commerce steps shown instead of self testing 2 56
Code not executing correctly. 3 43
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

734 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