|Classes of Roger Baklund
|PHP Indentation Tool
A few methods for working with text indentation.
Each method is described in more detail below.
Splits a string into blocks based on indentation.
This method takes a single argument, a string or an array of strings, like output
Note that using constant multiline strings in the PHP source code requires zero indentation for the root items, otherwise it would be seen as a single multiline block. This results in ugly source code:
Injects a number of WS characters at the start of each line in the string.
This method takes one, two or three arguments. The first is required, it is a
multiline string or an array of lines. The second is optional, the number of
whitespace characters to inject, the default value is 2. The third argument
is also optional, it is which whitespace character to use, by default it will
be a space. You could provide
Remove excessive indentation.
This method takes a single argument, a multiline string or an array of lines, and removes excessive whitespace from the start of each line. The first line is a special case: any indentation is removed and ignored. The first line with characters is allways considered to be at the "root level" of the collection of "blocks".
What is excessive indentation?
If all lines in the input (ignoring the first) have
For example, this:
...results in this:
Note that the structure is kept, the entire block of text is shifted to the left. "First" and "Second" has no indentation, lines two and four ("indented") are still indented, but now with only two space characters.
Sometimes the structure is changed, but only for the first line. Any leading whitespace will be removed and ignored when considering the indentation levels.
For example, this:
...is converted into this:
The difference is only in the indentation for the first line, which is handled in this special way for practical reasons.
Suppose you wanted Second and Third to be indented below First. This will not work:
The first two are identical, the linefeed before the ' does not matter. First, Second and Third are considered to be three items on the same level. The third example is less intuitive, the indentation is increased for Second and Third, but it is the same as the two above. All indentation for Second and Third is removed in all cases.
Even this next example is the same as the above, because the indentation for the first line is ignored:
If you wanted to keep the indentation in this case, you would have to leave the first line blank and Second and Third must have more indentation than First:
In the last two examples, Second and Third are indentated and 'belongs' to First, only the indentation for First and the initial blank line is removed.
Note that the problem descibed above only occurs when there is a single item with indented lines, both of the below examples will see this as two blocks; First with Second and Third indented, and Fourth:
Even the next example represents the same two blocks, because indentation for the first line is ignored:
Get indentation count for each line in a string with multipe lines.
This method takes a single argument; the text to analyze in the form of a multiline string or an array of lines. It will return an array of integers. Length of array corresponds to number of lines in the input, and each value represents the number of leading whitespace characters used for that line.
Note: TAB characters counts as one, just like space characters. If they are mixed the numbers does not reflect the indentation you can see in an editor.
Note: In addition to TAB and space characters, ASCII 0, 11 and 13 will also count as one.
Set indentation on individual lines in a multiline string.
This method takes two, three or four arguments. The first two are required.
The first is a multiline string or an array of lines. This is the text with
or without indentation. The second is an array of integers, each represents the
amount of whitespace you wish to set or append for each line. The third,
optional argument decides if the whitespace should be appended to existing
whitespace in the input sting, or if they represent the final indentation you
want in the result. The latter is the default, set it to
This would output something like this:
You can use the
Empty lines in the input would be returned as 0 indented. you can not distinguish
it from a line with text and no indentation without inspecting the line. Note that