【问题标题】:How to trim all whitespace from string, but preserve a single trailing line break if one exists如何从字符串中修剪所有空格,但如果存在则保留单个尾随换行符
【发布时间】:2017-02-17 16:31:40
【问题描述】:

我正在尝试创建一个简单的 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+$/,具体取决于是否存在换行符。关于在正则表达式中执行此操作的更简单、更优雅的方法有什么建议吗?

顺便说一句,这是我在 stackoverflow 上的第一篇文章

【问题讨论】:

    标签: php


    【解决方案1】:

    你可以这样做。 (见 cmets)

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

    【讨论】:

    • 太好了,我喜欢你的方法,但你的函数可以使用“\R”而不是“[\r\n]”来简化
    • @Talisphere 完美。所有测试仍然通过。我没有意识到 \R 也匹配 \r\n 。我会更新答案。
    【解决方案2】:

    简化...

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

    【讨论】:

      猜你喜欢
      • 2016-01-21
      • 2013-03-07
      • 1970-01-01
      • 2014-03-19
      • 1970-01-01
      • 2017-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多