Detect and Change Indentation With PHP


March 18th, 2022


The indentation package by Colin O'Dell is a PHP library to detect and manipulate indentation of strings and files:

This package is brand-new and I am not sure what the author's plans are long-term. I thought it was interesting enough to share if you need a package to detect (or change) the indentation type of a string or file:

1use ColinODell\Indentation\Indentation;
3$indentation = Indentation::detect(file_get_contents('composer.json'));
5$indentation->getAmount() === 4; // true
6$indentation->getType() === Indentation::TYPE_SPACE; // true
7(string)$indentation === ' '; // true

You can also change indentation using this package for file:

1use ColinODell\Indentation\Indentation;
3$composerJson = file_get_contents('composer.json');
4$composerJson = Indentation::change($composerJson, new Indentation(1, Indentation::TYPE_TAB));
5file_put_contents('composer.json', $composerJson);

You can check out this package on GitHub at colinodell/indentation to learn more and experiment with it.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.

Laravel News Partners