Enjoy an ad free experience by logging in. Not a member yet?
Register .
08-02-2007, 01:56 AM
PM User |
#1
Regular Coder
Join Date: Aug 2002
Location: Oregon, United States of America
Posts: 882
Thanks: 1
Thanked 9 Times in 9 Posts
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!!
08-02-2007, 02:15 AM
PM User |
#2
Regular Coder
Join Date: May 2006
Posts: 152
Thanks: 5
Thanked 0 Times in 0 Posts
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!
08-02-2007, 03:28 AM
PM User |
#3
Regular Coder
Join Date: Aug 2002
Location: Oregon, United States of America
Posts: 882
Thanks: 1
Thanked 9 Times in 9 Posts
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_r ( preg_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!!
08-02-2007, 08:16 AM
PM User |
#4
New to the CF scene
Join Date: Aug 2007
Location: Kolkata,India
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
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 );
?>
08-02-2007, 10:33 AM
PM User |
#5
New to the CF scene
Join Date: Aug 2007
Location: Kolkata,India
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
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 );
?>
08-02-2007, 11:01 PM
PM User |
#6
Regular Coder
Join Date: Aug 2002
Location: Oregon, United States of America
Posts: 882
Thanks: 1
Thanked 9 Times in 9 Posts
Thank you very much!
__________________
If I'm postin here, I NEED YOUR HELP!!
Jump To Top of Thread
Thread Tools
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
HTML code is Off
All times are GMT +1. The time now is 07:57 PM .
Advertisement
Log in to turn off these ads.