3v4l.org

run code in 500+ PHP versions simultaneously
<?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;
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.21, 8.5.0 - 8.5.7
section subsection text

preferences:
102.75 ms | 1255 KiB | 4 Q