如何从字符串中修剪所有空格,但如果存在则保留单个尾部换行符

时间:2016-10-08 22:49:16

标签: php

我正在尝试创建一个简单的php函数,其行为与php trim()函数相同,只是它保留了一个尾随换行符(如果存在)。此外,它需要支持保留CRLF,CR和LF案例。

考虑以下用例:

  1. myTrim(" \ t \ r \ n快速的棕色狐狸跳过懒狗\ t \ t \ t \ t \ {34; ) ==="快速的棕色狐狸跳过懒狗"
  2. myTrim(" \ t \ r \ n快速的棕色狐狸跳过懒狗\ t \ r \ n \ r \ n \ t \ r \ n" ) ==="快速棕色狐狸跳过懒狗\ r \ n"
  3. myTrim(" \ t \ r \ n快速的棕色狐狸跳过懒狗\ t \ r \ n \ r \ t \ r" ) ==="快速的棕色狐狸跳过懒狗\ R"
  4. myTrim(" \ t \ r \ n快速的棕色狐狸跳过懒狗\ t \ n \ n \ t \ n" ) ==="快速的棕色狐狸跳过懒狗\ n"
  5. 我没有成功尝试过如下函数:

    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+$/,具体取决于是否存在换行符。有关在regex中执行此操作的更简单,更优雅的方法的任何建议吗?

    BTW这是我对stackoverflow的第一篇文章

2 个答案:

答案 0 :(得分:1)

...简化

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");

答案 1 :(得分:0)

你可以这样做。 (见评论)

<?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)