如何从字符串中删除所有空白,但保留单个尾随换行符(如果存在的话)


How to trim all whitespace from string, but preserve a single trailing line break if one exists

我试图创建一个简单的php函数,其行为与php trim()函数相同,除了它保留了单个跟踪换行符(如果存在的话)。此外,它还需要支持保留CRLF、CR和LF情况。

考虑以下用例:

  1. myTrim( " 't 'r'n快速的棕色狐狸跳过懒狗't 't " ) === "快速的棕色狐狸跳过懒狗"
  2. myTrim( " 't 'r'n快速的棕色狐狸跳过懒惰的狗't 'r'n'r'n 't 'r'n" ) === "快速的棕色狐狸跳过懒惰的狗'r'n"
  3. myTrim( " 't 'r'n快速的棕色狐狸跳过懒惰的狗't 'r'r 't 'r" ) === "快速的棕色狐狸跳过懒惰的狗'r"
  4. myTrim( " 't 'r'n快速的棕色狐狸跳过懒惰的狗't 'n'n 't 'n" ) === "快速的棕色狐狸跳过懒惰的狗'n"

我尝试了一个函数,如:

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;
}

上面的代码似乎完全忽略了换行符,只匹配简单的大小写('s*)。我能看到的唯一另一种方法是使用"if"语句首先测试/'s*'R's*$/模式,然后使用/'s*'R's*$//'s+$/,这取决于是否存在换行符。在正则表达式中有什么更简单、更优雅的方法吗?

BTW这是我在stackoverflow上的第一篇文章

简化…

function myTrim($str) {
    // if trailing line break exists, preserve it
    if ( preg_match("/'s*?('R)'s*$/", $str, $capture) ) {
        // return the trimmed string, plus the line break
        return trim($str) . $capture[1];
    }
    // No line break was found, return the trimmed string
    return trim($str);
}
var_dump(myTrim(" 't 'r'n The quick brown fox jumps over the lazy dog 't 't ") === "The quick brown fox jumps over the lazy dog");
var_dump(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");
var_dump(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");
var_dump(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");
//Added a couple more edge test cases
var_dump(myTrim(" 't 'r'n The quick brown fox jumps over the lazy dog 't 'n'r'n 't 'n ") === "The quick brown fox jumps over the lazy dog'n");
var_dump(myTrim(" 't 'r'n The quick brown fox jumps over the lazy dog 't 'r'n'n 't 'n ") === "The quick brown fox jumps over the lazy dog'r'n");

您可以这样做。(见注释)

<?php
function myTrim($str) {
    //Look for a non whitespace character with whitespace following.
    //Capture the first (either 1 or 2) consecutive line ending characters
    if (preg_match("/[^'s]'s*?('R)'s*$/", $str, $capture)) {
        // return the trimmed string, plus the line break
        return trim($str) . $capture[1];
    }
    //No line endings were found
    return trim($str);
}
var_dump(myTrim(" 't 'r'n The quick brown fox jumps over the lazy dog 't 't ") === "The quick brown fox jumps over the lazy dog");
var_dump(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");
var_dump(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");
var_dump(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");
//Added a couple more edge test cases
var_dump(myTrim(" 't 'r'n The quick brown fox jumps over the lazy dog 't 'n'r'n 't 'n ") === "The quick brown fox jumps over the lazy dog'n");
var_dump(myTrim(" 't 'r'n The quick brown fox jumps over the lazy dog 't 'r'n'n 't 'n ") === "The quick brown fox jumps over the lazy dog'r'n");
//Outputs
//bool(true) bool(true) bool(true) bool(true) bool(true) bool(true)