Current location: Hot Scripts Forums » Programming Languages » PHP » Stripping multiline comments using RegEx


Stripping multiline comments using RegEx

Reply
  #1 (permalink)  
Old 06-07-05, 11:02 PM
REMIYA's Avatar
REMIYA REMIYA is offline
Newbie Coder
 
Join Date: Jun 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation Stripping multiline comments using RegEx

I want to strip the single line and multi line comments wrapped in /* */.

The following RegEx does exatly that, but strips the comments also and in the strings.



PHP Code:



 

//Strips all the multi-line and single-line /* */comments even in strings ""

$pattern ="/\/\\*[\\s\\S]*?\\*\//";

$replacement "THE COMMENT HAS BEEN HERE";

echo 
nl2br(preg_replace($pattern$replacement$phpcode)); 


So an initial code of:

PHP Code:



 

/*

This is a multiline comment

*/

 

/* This is a single line comment */

 

echo "Hi, /* This is a single line comment in a string*/"


Processed through the above RegEx returns:

PHP Code:

 THE COMMENT HAS BEEN HERE


 

THE COMMENT HAS BEEN HERE

 

echo "Hi, THE COMMENT HAS BEEN HERE"


I've done a lot of testing with RegExs, but all was in vain. So was the search through Internet.

Is there any solution to the problem using RegEx?

Please, test before you post.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-08-05, 04:38 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-08-05, 07:28 AM
dennispopel dennispopel is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 263
Thanks: 0
Thanked 0 Times in 0 Posts
__________________
onPHP5.com - PHP5: Articles, News, Tutorials, Interviews, Software and more
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 06-08-05, 10:14 AM
REMIYA's Avatar
REMIYA REMIYA is offline
Newbie Coder
 
Join Date: Jun 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by NeverMind
Thank you, I haven't seen this by now
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 06-08-05, 10:24 AM
REMIYA's Avatar
REMIYA REMIYA is offline
Newbie Coder
 
Join Date: Jun 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by dennispopel
This function is very good. But it takes a file.

So I'll have to save the source code to a temporary file, proceed it through this function, and grab the result.

Is there a dynamic solution, as to pass the source code as a String?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 06-09-05, 08:20 AM
dennispopel dennispopel is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 263
Thanks: 0
Thanked 0 Times in 0 Posts
Hello,

Unfortuanately many functions in PHP work this stupid way - only thru a file name. If PHP had some nice streams framework like Java has, it would be possible to pipe the string thru a memory stream to this function.
__________________
onPHP5.com - PHP5: Articles, News, Tutorials, Interviews, Software and more
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
looking for comments system orang3 Script Requests 2 03-22-05 10:32 AM
regex and VB Net warpminded Windows .NET Programming 0 12-26-04 08:17 AM
Abolish comments in java code Mr Badger Everything Java 1 12-06-04 11:48 AM
regex, need to put found value in replacement brackets windwind PHP 5 02-15-04 09:45 PM


All times are GMT -5. The time now is 09:14 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.