Add indents to multiline text

@param string $text
@param integer $indent indent in spaces
@param bool $skipFirstLine = false not apply indent to the first line of text
@return array
331233
public static function indentLines($text, $indent, $skipFirstLine = false)
    {
        $lines = explode(PHP_EOL, $text);

        // (つ◕.◕)つ━☆ﾟ.*･｡ﾟ
        !$skipFirstLine || $preparedLines[] = array_shift($lines);

        foreach ($lines as $line) {
            $preparedLines[] = self::indent($indent) . rtrim($line);
        }

        return implode(PHP_EOL, $preparedLines);
    }
1254095
protected function printp($input, $indent = 0) {
		$input = trim($input, "\n\t\r ");
		$indentation = str_repeat(' ', $indent);
		$this->cli->printf($indentation.wordwrap(str_replace("\n", "\n$indentation", $input), 75 - $indent, "\n$indentation")."\n");
	}
884462
private static int skipEndOfLine(String text, int offset)
   {
      char c;
      boolean finished = false;

      while (finished == false)
      {
         c = text.charAt(offset);
         switch (c)
         {
            case ' ': // found that OBJDATA could be followed by a space the EOL
            case '\r':
            case '\n':
            {
               ++offset;
               break;
            }

            case '}':
            {
               offset = -1;
               finished = true;
               break;
            }

            default:
            {
               finished = true;
               break;
            }
         }
      }

      return (offset);
   }
294394
static function reindent($source, $indent = '', $tabs_to_spaces = 4, $ignore_zero_indent = true) {
		// unify tabs
		if ($tabs_to_spaces !== null) {
			$source = str_replace("\t", str_repeat(' ', $tabs_to_spaces), $source);
		}

		// split into lines
		$lines = explode("\n", $source);

		// detect indent level
		$min_length = null;
		foreach ($lines as $line) {
			if (preg_match('#^([ \t]+)([^ ])#', $line, $matches)) {
				if ($min_length === null) {
					$min_length = strlen($matches[1]);
				}
				else if (strlen($matches[1]) < $min_length) {
					$min_length = strlen($matches[1]);
				}
			}
			else if ( ! $ignore_zero_indent) {
				$min_length = 0;
				break;
			}
		}

		// unify
		foreach ($lines as &$line) {
			if (preg_match('#^[ \t].*#', $line)) {
				$line = $indent.substr($line, $min_length);
			}
			else { # zero line
				$line = $indent.$line;
			}
		}

		return implode("\n", $lines);
	}
456240
public function createToc($text, $start = 2, $stop = 4)
    {
        $level = "$start-$stop";
        $pattern = "#<(h[$level])([^>]*)>(.*)</h[$level]>#";
        preg_match_all($pattern, $text, $matches, PREG_SET_ORDER);

        $toc = [];
        foreach ($matches as $val) {
            preg_match("#id=['\"]([^>\"']+)#", $val[2], $id);
            $id = isset($id[1]) ? $id[1] : null;
            $toc[] = [
                "level" => isset($val[1])
                    ? $val[1]
                    : null,
                "title" => isset($val[3])
                    ? ltrim(strip_tags($val[3]), "#")
                    : null,
                "id" => $id,
            ];
        }

        return $toc;
    }
882059
def indent_lines(text, indent)
      text ||= ''
      text.lines.map { |line| indent + line }.join
    end
713605
private static function formatTags(array $skip, array $tags)
    {
        $chunks = [];

        foreach ($tags as $name => $values) {
            if (\in_array($name, $skip)) {
                continue;
            }

            foreach ($values as $value) {
                $chunks[] = \sprintf('<comment>%s%s</comment> %s', self::inflect($name), empty($value) ? '' : ':', OutputFormatter::escape($value));
            }

            $chunks[] = '';
        }

        return \implode("\n", $chunks);
    }
1162155
function trim(text) {
  var MAX_INDENT = 9999;
  var empty = RegExp.prototype.test.bind(/^\s*$/);
  var lines = text.split('\n');
  var minIndent = MAX_INDENT;
  var indentRegExp;
  var ignoreLine = (lines[0][0] != ' '  && lines.length > 1);
  // ignore first line if it has no indentation and there is more than one line

  lines.forEach(function(line){
    if (ignoreLine) {
      ignoreLine = false;
      return;
    }

    var indent = line.match(/^\s*/)[0].length;
    if (indent > 0 || minIndent == MAX_INDENT) {
      minIndent = Math.min(minIndent, indent);
    }
  });

  indentRegExp = new RegExp('^\\s{0,' + minIndent + '}');

  for ( var i = 0; i < lines.length; i++) {
    lines[i] = lines[i].replace(indentRegExp, '');
  }

  // remove leading lines
  while (empty(lines[0])) {
    lines.shift();
  }

  // remove trailing
  while (empty(lines[lines.length - 1])) {
    lines.pop();
  }
  return lines.join('\n');
}
1339271
def indent (text, indent_string="  "):
    """"""
    lines = str(text).splitlines()
    return os.linesep.join("%s%s" % (indent_string, x) for x in lines)
210577
protected function onFormatText(string &$text): void
    {
        $lines = preg_split("/\r\n|\n|\r/", $text);
        $result = [];

        $lastEmptyLinesCount = 0;
        $hasFoundNonEmptyLines = false;

        foreach ($lines as $line) {
            if (($this->textFormatOptions & TextFormatOptions::TRIM) !== 0) {
                $line = trim($line);
            }
            if (($this->textFormatOptions & TextFormatOptions::COMPACT) !== 0) {
                $line = preg_replace('/\s+/', ' ', $line);
            }

            if ($line === '') {
                if (($this->textFormatOptions & TextFormatOptions::COMPACT_LINES) !== 0 && $lastEmptyLinesCount > 0) {
                    continue;
                }
                if (($this->textFormatOptions & TextFormatOptions::TRIM_LINES) !== 0 && !$hasFoundNonEmptyLines) {
                    continue;
                }

                $lastEmptyLinesCount++;
            } else {
                $lastEmptyLinesCount = 0;
                $hasFoundNonEmptyLines = true;
            }

            $result[] = $line;
        }

        if (($this->textFormatOptions & TextFormatOptions::TRIM_LINES) !== 0 && $lastEmptyLinesCount > 0) {
            // Trim the last empty lines (we did not know until now that those were at the end).
            array_splice($result, -$lastEmptyLinesCount);
        }

        $text = implode("\r\n", $result);
    }
775026
private function text($text, $level)
    {
        $indentNextLine = (substr($text, -1) === "\n");
        $code = sprintf($this->prepare(self::TEXT, $level), $this->flushIndent(), var_export($text, true));
        $this->indentNextLine = $indentNextLine;

        return $code;
    }
719825
public function numberOfLines($width, $text) {
    $nbLines = 0;
    $lines = preg_split('/\n/', $text);
    foreach ($lines as $line) {
      $nbLines += $this->NbLines($width, $line);
    }
    return $nbLines;
  }
94791
protected function addIndent(string $string, int $indent = 0): string
    {
        return str_repeat(self::INDENT, max($indent, 0)) . $string;
    }
1100909
public function addRawFromAssocArray($lines, $indentation = 0)
	{
		foreach ($lines as $key => $value)
		{
			if (is_array($value))
			{
				$this->addLine($key . ': [', array("indentation"=>$indentation));
				$this->addRawFromAssocArray($value, $indentation + 1);
				$this->addLine(']', array("indentation"=>$indentation));
			}
			else
			{
				$this->addLine($key . ': ' . $value, array("indentation"=>$indentation));
			}
		}
	}
297515
private static function linebreakSplit($text)
    {
        $lines = [];
        $line = '';

        foreach (Multibyte::split('([\r\n]+)', $text, -1, PREG_SPLIT_DELIM_CAPTURE) as $part) {
            $line .= $part;
            if (Multibyte::trim($part) === '') {
                $lines[] = $line;
                $line = '';
            }
        }
        $lines[] = $line;

        return $lines;
    }
368905
public static function indent( $lines, $cnt = 1 )
	{
		$new = [];
		$lines = str_contains( $lines, '\r\n' ) ? explode( "\r\n", $lines ) : explode( "\n", $lines );

		foreach ( $lines as $line )
			$new[] = str_repeat( "\t", $cnt ) . $line;

		return implode( "\n", $new );
	}
626574
public static function explode ($string, $delimiter = ',', $trim = true, $skipEmpty = true)
		{
			return parent::explode($string, $delimiter, $trim, $skipEmpty);
		}