【问题标题】:How to remove all white spaces if more than one如果多个空格如何删除所有空格
【发布时间】:2016-03-30 08:31:26
【问题描述】:

我正在尝试从 html 内容中删除 PHP 中的所有空格,如果它长于一个,例如这样的链:

{{IMG}}                                     {{IMG}}                                     {{IMG}}                                     {{IMG}}                                     {{IMG}}

但不应影响以下句子:关于我们。

应该用正则表达式来完成吗?有什么想法吗?

PS:该变量已与 trim() 一起使用;它会删除末尾和开头的空格,但不会删除字符之间的空格...

非常感谢您的帮助。

【问题讨论】:

  • 使用trim(string);函数
  • 你的意思是,就像一个 trim() 函数?
  • Matt Magallo 所说,如果内容在变量中,则使用 trim();
  • @MattMagallo trim();删除末尾和开头的空格,因此如果字符之间有空格,它们将不会被删除...
  • 是的,它们在 foreach 循环中

标签: php html regex


【解决方案1】:

$str = preg_replace('/\s+/', ' ', $originalString); echo $str;

这将用一个空格替换所有空格。

【讨论】:

  • 没有解决问题,但找到了一种使其与您的代码一起工作的方法。非常感谢!
  • 酷。你能分享你的发现吗?将来会对某人有所帮助。
  • 使用您的代码将输出以下内容:{{IMG}} {{IMG}} {{IMG}} {{IMG}} {{IMG}} 如果您在我的帖子中引用我的变量,所以我应用了另一个正则表达式: preg_replace("/}} {{/", "}}{{", $str);所以我能够判断句子是否只是变量。谢谢
  • @GuilVII 为什么这个解决方案不能解决您的问题?它完全按照您的要求进行。如果现在是您需要的,请先提出正确的问题。
【解决方案2】:

如果你问我,这是一个相当奇怪的要求。

您可能会做类似的事情(从接受的答案中采用,以防其他人发现它有用):

$str = preg_replace('/\s\s+/', '', $originalString);

如果连续至少有 2 个空格,这将删除所有空格。

【讨论】:

    【解决方案3】:

    你可以这样做:

    示例 1:如果大于 1,则删除空格

    <?php
    # Test variable
    $string = "HELLO     I      HAVE    WHITE SPACEEE!!1";
    
    # Count all whitespaces
    # Contains more than 1 whitespace
    if(substr_count($string, ' ') > 1) {
        # Example 1: Remove the whitespaces
    
        $string = preg_replace('/\s+/', '', $string);
    }
    # Ouput: HELLOIHAVEWHITESPACEEE!!1
    echo $string;
    ?>
    

    示例 2:修剪空格

    <?php
    # Test variable
    $string = "HELLO     I      HAVE    WHITE SPACEEE!!1";
    
    # Count all whitespaces
    # Contains more than 1 whitespace
    if(substr_count($string, ' ') > 1) {
        # Example 2: Remove the whitespaces
    
        $string = trim($string);
    }
    # Ouput: HELLO I HAVE WHITE SPACEEE!!1
    echo $string;
    ?>
    

    示例 3:替换空格

    <?php
    # Test variable
    $string = "HELLO     I      HAVE    WHITE SPACEEE!!1";
    
    # Count all whitespaces
    # Contains more than 1 whitespace
    if(substr_count($string, ' ') > 1) {
        # Example 1: Replace the whitespaces
    
        $string = preg_replace('/\s+/', ' ', $string);
    }
    # Ouput: HELLO I HAVE WHITE SPACEEE!!1
    echo $string;
    ?>
    

    示例 4:

    <?php
    # Test variable
    $string = "HELLO     I     HAVE     WHITE     SPACEEE!!1 And I like bananas.";
    
    # Count all whitespaces
    # Contains more than 1 whitespace
    if(substr_count($string, ' ') > 1) {
        # Example 1: Replace the whitespaces
    
        $string = preg_replace('/\s\s+/', '', $string);
    }
    # Ouput: HELLOIHAVEWHITESPACEEE!!1 And I like bananas.
    echo $string;
    ?>
    

    【讨论】:

    • 这可行,但问题是像“我吃香蕉”这样的句子包含 2 个空格,它们将被删除,但它们不应该......
    • @MattMagallo facepalm?
    • 示例 #2 显然不会产生它声称的输出。示例 #1 不会产生问题中要求的输出。
    • @axiac 这些是可能的解决方案,他已经找到了。这个答案也可以帮助其他人,所以我在这里看不到问题吗?
    猜你喜欢
    • 2014-02-18
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 2011-01-20
    • 2017-07-21
    • 2019-08-13
    • 2014-06-19
    相关资源
    最近更新 更多