Download the PHP package colinodell/indentation without Composer
On this page you can find all versions of the php package colinodell/indentation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download colinodell/indentation
More information about colinodell/indentation
Files in colinodell/indentation
Package indentation
Short Description Library to detect and manipulate indentation in strings and files
License MIT
Homepage https://github.com/colinodell/indentation
Informations about the package indentation
indentation
PHP library to detect and manipulate the indentation of files and strings
Installation
Usage
Detecting the indentation of a string or file
Changing the indentation of a string or file
Adding leading indentation to all lines
Need to add indent all lines by some amount?
Now you can embed the indented code into a Markdown document! (Hint: This works great with the league/commonmark library.)
Removing leading indentation from all lines
Imagine you have a file where every line is indented by at least 4 spaces:
You can trim that leading indentation while preserving the nested indentation with the unindent()
method:
Giving you:
Note how the leading 4 spaces are removed but all other indentation (like in the docblock and method body) is preserved.
Detection Algorithm
The current algorithm looks for the most common difference between two consecutive non-empty lines.
In the following example, even if the 4-space indentation is used 3 times whereas the 2-space one is used 2 times, it is detected as less used because there were only 2 differences with this value instead of 4 for the 2-space indentation:
Furthermore, if there are more than one most used difference, the indentation with the most lines is selected.
In the following example, the indentation is detected as 4-spaces: