Current location: Hot Scripts Forums » Programming Languages » PHP » The perfect PHP comments strip regex?


The perfect PHP comments strip regex?

Reply
  #1 (permalink)  
Old 01-05-06, 07:40 AM
Subway Subway is offline
New Member
 
Join Date: Jan 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
The perfect PHP comments strip regex?

I'm working on a php application that scans through php files (phpPackageScan). Here's a sample output of a scan (WordPress 1.5): http://www.osxcode.com/phpPackageScans/wordpress/

One of the things the script does before it searches for functions and cross references is strip comments. This is the regex I'm using:

PHP Code:

$content preg_replace('@(/\*.*?\*/)@se'"remove_non_linebreaks('\\1',$step)"$content);

                        
$content preg_replace('/(#+[^\n]*?\n)/se'"remove_non_linebreaks('\\1',$step)"$content);
                        
$content preg_replace('@(//+([^\n]*?\n))@se'"remove_non_linebreaks('\\1',$step)"$content); 
That function removes the comments without removing the linebreaks. Now the problem is that if the php file contains something like:

PHP Code:

if (preg_match('#^/*' $this->index '#'$this->permalink_structure)) { 

And another /* */ comment somewhere below, it will strip everything between even if it isn't a comment ... obviously not a good thing.

In the above linked scan, you can see the problem if you view the comment of the rewrite_rules function inside /wp-includes/classes.php

Has anyone an idea how I could make that regex act more perfect? If possible without making the script too slow?

Fredi
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
PHP multi-dimensional array sorting issue aqw PHP 2 06-24-05 11:09 PM
Stripping multiline comments using RegEx REMIYA PHP 5 06-09-05 07:20 AM
PHP Comments omgwtf PHP 2 03-20-04 03:26 PM
PHP Comments Script JavaHead Script Requests 0 09-21-03 07:49 AM
Help with Regex in PHP HS Staff PHP 5 06-07-03 01:32 AM


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