I am trying to create a simple php function to behave the same as the php trim()
function, except that it preserves a single trailing line break if one exists. In addition, it needs to support preserving CRLF, CR and LF cases.
Consider the following use cases:
myTrim(
" \t \r\n The quick brown fox jumps over the lazy dog \t \t ")
=== "The quick brown fox jumps over the lazy dog"myTrim(
" \t \r\n The quick brown fox jumps over the lazy dog \t \r\n\r\n \t \r\n ")
=== "The quick brown fox jumps over the lazy dog\r\n"myTrim(
" \t \r\n The quick brown fox jumps over the lazy dog \t \r\r \t \r ")
=== "The quick brown fox jumps over the lazy dog\r"myTrim(
" \t \r\n The quick brown fox jumps over the lazy dog \t \n\n \t \n ")
=== "The quick brown fox jumps over the lazy dog\n"
I have unsuccessfully tried a function such as:
public static function trimMessageBlock( $block )
{
// Remove all leading whitespace (e.g. HT (9), LF (10), FF (12), CR (13), and space (32))
$block = preg_replace("/^\s+/", "", $block);
// Remove all trailing whitespace, but preserve a single trailing line break if one exists
$block = preg_replace("/\s*(\R?)\s*$/", "$1", $block);
return $block;
}
The above code seems to ignore the line break character entirely, and match only the simple case (\s*
). The only other way I can see to do this is to use a "if" statement to test for a /\s*\R\s*$/
pattern first, then either use /\s*\R\s*$/
or /\s+$/
, depending on if a newline is present of not. Any suggestions on a simpler, more elegant way to do this in regex?
BTW This is my first post to stackoverflow