Skip to content

Instantly share code, notes, and snippets.

@reinink
Created September 24, 2019 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save reinink/0648d8e32bfa65fdaf50cdd813f10f77 to your computer and use it in GitHub Desktop.
Save reinink/0648d8e32bfa65fdaf50cdd813f10f77 to your computer and use it in GitHub Desktop.
<?php
function trim_leading_whitespace($code)
{
$lines = collect(explode("\n", $code));
$min = collect(explode("\n", $code))->filter(function ($line) {
return trim($line);
})->min(function ($line) {
return strlen($line) - strlen(ltrim($line));
});
$code = $lines->map(function ($line) use ($min) {
return substr($line, $min);
})->implode("\n");
return htmlspecialchars(trim($code));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment