<?php // the input $input = <<<STR section subsection text STR; // determine initial whitespace length preg_match('/^(?<initial>\s*)/', $input, $match); ['initial' => $initialWhiteSpace] = $match; $whiteSpaceLength = strlen($initialWhiteSpace); // convert string to array of lines $lines = explode(PHP_EOL, $input); // replace the white-space $output = array_map( static fn (string $str): string => preg_replace(sprintf("/^[ \t]{%d}/", $whiteSpaceLength), '', $str), $lines, ); // recreate the string $output = implode(PHP_EOL, $output); echo $output;
You have javascript disabled. You will not be able to edit any code.