Created
September 24, 2019 18:04
-
-
Save reinink/0648d8e32bfa65fdaf50cdd813f10f77 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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