A high-parity universal comment stripper available in both Python and PHP.
It handles file types robustly and natively without altering code integrity, correctly handling edge cases like internet-explorer conditional comments, literal strings, nested templating tags, and regular expressions.
- Extensive Language Support: Precisely strips comments in PHP, Blade (
.blade.php), JavaScript, JSX, TypeScript, TSX, CSS, SCSS, Python, HTML, Jinja2, and Twig. - Literal Safety: Safe string (
",',`) and regex literal (/regex/) handling to avoid false positives. It natively distinguishes//inside a URL (http://) versus an actual line comment. - Native Tokenization: Uses Python's native
tokenizefor.pyfiles and PHP's nativetoken_get_all()for.phpfiles to ensure zero false positives parsing complex syntax. - Embedded Block Support: Seamlessly processes script headers, style blocks, and Blade
@phpinclusions in HTML structures (<script>,<style>). Honors exact contents of@verbatim. - 100% Parity: Python and PHP versions behave identically and output exact byte-for-byte structures.
- Zero Dependencies: Entirely self-contained requiring no external requirements or libraries (No
tree_sitterrequired).
- Python script (
universal_stripper.py): Python 3.6+ - PHP script (
universal_stripper.php): PHP 8.0+
Note on Cross-Language Capabilities: For 100% syntactical safety, when the Python script encounters a
.phpfile, it usessubprocessto callphp -rfor PHP's native tokenizer. Similarly, when the PHP script encounters a.pyfile, it callspython3 -cfor Python's native tokenizer. If the corresponding CLI is missing, it falls back to a highly reliable C-style regex-based replacement mechanism.
Both scripts expose identical arguments.
python universal_stripper.py <target_path> [OPTIONS]php universal_stripper.php <target_path> [OPTIONS]| Argument | Description |
|---|---|
<target_path> |
(Required) The root file or directory to process. If a directory, the stripper recursively scans all supported extension files. |
--dry-run |
Prints which files would be updated, but makes absolutely zero modifications to the actual files. |
--collapse-blank-lines <int> |
Default: 2. The maximum number of consecutive blank lines allowed. Use -1 to disable collapsing blank lines. (Reduces bloated whitespace typically left behind when removing huge multi-line documentation comments). |
--no-whitespace |
Disables trimming trailing spaces and disables collapsing blank lines entirely. Structural empty spaces where comments used to be will remain completely untouched. |
--keep-directive <regex> |
Specify a comment body regex to preserve. For example, use --keep-directive .*eslint-disable.* to avoid stripping linter directives. Can be used multiple times. |
--exclude <glob> |
Globs matched against the relative path. Use this to exclude custom folders. Note: node_modules, vendor, .git, coverage, and .venv are ignored by default automatically! |
--exclude-regex <regex> |
Provides exclusion mapping using actual Regular Expressions instead of simple globs for complex exclusions. |
Process a single file or an entire directory safely.
Python:
python universal_stripper.py resources/views/PHP:
php universal_stripper.php resources/views/Check which files would be modified without actually changing them.
Python:
python universal_stripper.py app/Models/User.php --dry-runPHP:
php universal_stripper.php app/Models/User.php --dry-runChange the maximum consecutive blank lines left behind (default is 2). Set to 0 to heavily compress vertical space.
Python:
python universal_stripper.py src/ --collapse-blank-lines 1PHP:
php universal_stripper.php src/ --collapse-blank-lines 0Strips comments but leaves all resulting empty lines and spaces perfectly intact (useful for debugging line mapping).
Python:
python universal_stripper.py public/js/app.js --no-whitespacePHP:
php universal_stripper.php public/js/app.js --no-whitespaceProtect specific comments like linter directives or type annotations. Can be passed multiple times.
Python:
python universal_stripper.py resources/js --keep-directive ".*eslint-disable.*" --keep-directive ".*@ts-expect-error.*"PHP:
php universal_stripper.php resources/js --keep-directive ".*eslint-disable.*" --keep-directive ".*@ts-expect-error.*"Exclude specific paths using glob patterns (appended to default ignores like vendor/, node_modules/).
Python:
python universal_stripper.py ./ --exclude "tests/Feature/**" --exclude "*.min.js"PHP:
php universal_stripper.php ./ --exclude "tests/Feature/**" --exclude "*.min.js"Exclude specific paths using complex regular expressions.
Python:
python universal_stripper.py ./ --exclude-regex "^tests\/Fixtures\/.*"PHP:
php universal_stripper.php ./ --exclude-regex "^tests\/Fixtures\/.*"