PHP - Remove excess Whitespace but not new lines

后端 未结 2 1566
伪装坚强ぢ
伪装坚强ぢ 2021-01-02 08:54

i was looking for a way to remove excess whitespaces from within a string (that is, if 2 or more spaces are next each other, leave only 1 and remove the others), i found thi

相关标签:
2条回答
  • 2021-01-02 09:15

    if you want to remove excess of only-spaces (not tabs, new-lines, etc) you could use HEX code to be more specific:

        $text = preg_replace('/\x20+/', ' ', $text);
    
    0 讨论(0)
  • 2021-01-02 09:18

    http://www.php.net/manual/en/regexp.reference.escape.php defines \h any horizontal whitespace character (since PHP 5.2.4) so probably you are looking for

    $foo = preg_replace( '/\h+/', ' ', $foo );

    example: http://ideone.com/NcOiKW

    0 讨论(0)
提交回复
热议问题