Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-02-2007, 01:56 AM   PM User | #1
Ultragames
Regular Coder

 
Join Date: Aug 2002
Location: Oregon, United States of America
Posts: 882
Thanks: 1
Thanked 9 Times in 9 Posts
Ultragames has a little shameless behaviour in the past
Removing comments

I need to have PHP read another file, and do two things (at least there are two that I don't know how to do.)

1) Delete anything after and including a // but only on that line. The line does not need to start with the // but if it exists, everything on the line after the // and including the // need to go.

2) Delete anything inbetween and including a /* and */ on any number of lines.

Basicly what I am doing is removing any comments from a JavaScript file. I am also replacing new lines and tabs with spaces, however I don't need help with that part of it.

Thank you for your help in figuring this one out.
__________________
If I'm postin here, I NEED YOUR HELP!!
Ultragames is offline   Reply With Quote
Old 08-02-2007, 02:15 AM   PM User | #2
FuZion
Regular Coder

 
Join Date: May 2006
Posts: 152
Thanks: 5
Thanked 0 Times in 0 Posts
FuZion is an unknown quantity at this point
I would use regex in the preg_replace function.

This regex should work: /\/\*(.+)\*\//
I'm not the best with this stuff so someone may have a better way.. I'm pretty sure that'll work though.. hope this helps!
FuZion is offline   Reply With Quote
Old 08-02-2007, 03:28 AM   PM User | #3
Ultragames
Regular Coder

 
Join Date: Aug 2002
Location: Oregon, United States of America
Posts: 882
Thanks: 1
Thanked 9 Times in 9 Posts
Ultragames has a little shameless behaviour in the past
Your regex dosn't appear to do anything at all...

PHP Code:
$var = <<<HEREDOC
        var code = 'test'; // Remove me!
        /*
            Remove this too
        */
        var code = 'test';
HEREDOC;

    
print_rpreg_replace('/\/\*(.+)\*\//'''$var) ); 
Returns:

Code:
var code = 'test'; // Remove me!
/*
	Remove this too
*/
var code = 'test';
And should return:

Code:
var code = 'test';
var code = 'test';
Did I miss a step?
__________________
If I'm postin here, I NEED YOUR HELP!!
Ultragames is offline   Reply With Quote
Old 08-02-2007, 08:16 AM   PM User | #4
s_pradeep
New to the CF scene

 
Join Date: Aug 2007
Location: Kolkata,India
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
s_pradeep is an unknown quantity at this point
Cool

You'll need to different regexes, one for single line comments and one for multi-line comments.

PHP Code:
<?php
$var 
= <<<HEREDOC
        var code = 'test'; // Remove me!
        /*
            Remove this too
        */
        var code = 'test';
HEREDOC;

    
$var preg_replace('!//.+!'''$var);  
    
$var preg_replace('!/\*.+\*/!s'''$var);  

    
printf('<pre>%s</pre>',$var);
?>
s_pradeep is offline   Reply With Quote
Old 08-02-2007, 10:33 AM   PM User | #5
s_pradeep
New to the CF scene

 
Join Date: Aug 2007
Location: Kolkata,India
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
s_pradeep is an unknown quantity at this point
Cool

A small fix here...

PHP Code:
<?php
$var 
= <<<HEREDOC
        var code = 'test'; // Remove me!
        /*
            Remove this too
        */

        if(test) { callem();/* one more here */ return 1;} // test ok
        /* hahahah */
        var code = 'test';
HEREDOC;

    
//printf('<pre>%s</pre>',$var);

    
$var preg_replace('!//.+!'''$var);  
    
$var preg_replace('!/\*[^/\\*]+\*/!s'''$var);  

    
printf('<pre>%s</pre>',$var);
?>
s_pradeep is offline   Reply With Quote
Old 08-02-2007, 11:01 PM   PM User | #6
Ultragames
Regular Coder

 
Join Date: Aug 2002
Location: Oregon, United States of America
Posts: 882
Thanks: 1
Thanked 9 Times in 9 Posts
Ultragames has a little shameless behaviour in the past
Thank you very much!
__________________
If I'm postin here, I NEED YOUR HELP!!
Ultragames is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:57 PM.


Advertisement
Log in to turn off these ads.