Skip to content

Instantly share code, notes, and snippets.

@fuzzyfox
Created September 22, 2020 19:09
Show Gist options
  • Save fuzzyfox/4f2f2fc4b633c4f5e4ba0a5702f58492 to your computer and use it in GitHub Desktop.
Save fuzzyfox/4f2f2fc4b633c4f5e4ba0a5702f58492 to your computer and use it in GitHub Desktop.
Remove trailing whitespace from an input string (multiline).
function removeTrailingWhitespace(string $string) : string
{
return preg_replace('/[ \t]+(\r?)$/m', '$1', $string);
}
// PHPUnit testcase for method
// public function testTestInternalRemoveTrailingWhitespace()
// {
// $this->assertSame(
// "\n some\r\n string\n\n\r\nto remove trailing\n whitespace from.\n\n",
// removeTrailingWhitespace("\n some \r\n string\n \n \r\nto remove trailing \n whitespace from. \n\n")
// );
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment