2

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 start 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?

psion
  • 748
  • 1
  • 6
  • 17

2 Answers2

4

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).

Gordon
  • 312,688
  • 75
  • 539
  • 559
3
$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));
Matthew
  • 47,584
  • 11
  • 86
  • 98
  • +1 works and is better than mine. could use /x though for some inline documentation so you still understand it when reading it in three months. – Gordon Apr 04 '11 at 19:20
  • 1
    @Gordon, that's good suggestion, but I won't be using this code in three months and it's a good exercise for the reader to add the comments himself. ;) (Truthfully, I'm just lazy.) – Matthew Apr 04 '11 at 19:39
  • thanks for the help. I'm using the last code snippit in my code. Thank you again. – psion Apr 05 '11 at 06:58