Stripping Comments out of a file in PHP
I have to parse various files that contain comments of differing types. I have been trying to figure an easy way to strip out these comments, but nothing good so far. I have full line comments that st开发者_开发知识库art with both # and ; and comments that start after relevant data. Best example:
data
data
#comment
;comment
data ;comment
data #comment
Any help in stripping these comments? And perhaps blank lines as well?
This should work (live demo):
echo preg_replace(
'/
[;#] # remove strings starting with ; or #
.* # and everything following that
$ # until it ends in a newline
/mx', // make it span multilines
'',
$data
);
The above will leave blanks lines where full line comments have been. If you want to replace those as well, add \s
before the first [;#]
(as shown by @konforce below).
$t = ltrim(preg_replace('/\s*[#;].*$/m', '', $t));
Should work. Removes all comments, making sure not to leave any blank lines where comments used to be. Also kills whitespace before comments, but that can be changed (\s
to \n
) if you don't want that.
Edit: Just saw the note regarding removing blank lines. The following should remove both comments and blank lines:
$t = ltrim(preg_replace('/(\s*[#;].*$)|(^\s*)/m', '', $t));
Untested, but the second condition should catch blank (only whitespace) lines. The ltrim
is still necessary to remove any whitespace that a leading comment may have caused. Possibly could catch that as part of the regexp, but I think it's less complex with an ltrim
.
Edit Again: Actually the above would remove all leading whitespace on each line. If that's a problem, you could fix it with:
$t = ltrim(preg_replace('/(\s*[#;].*$)|(^\s*\n)/m', '', $t));
精彩评论