删除多个空格

时间:2010-02-24 13:02:06

标签: php regex string preg-replace whitespace

我从MySQL数据库中获取$row['message'],我需要删除所有空格,例如\n \t,依此类推。

$row['message'] = "This is   a Text \n and so on \t     Text text.";

应格式化为:

$row['message'] = 'This is a Text and so on Text text.';

我试过了:

 $ro = preg_replace('/\s\s+/', ' ',$row['message']);
 echo $ro;

但它不会删除\n\t,只删除单个空格。谁能告诉我怎么做?

16 个答案:

答案 0 :(得分:353)

你需要:

$ro = preg_replace('/\s+/', ' ',$row['message']);

您正在使用\s\s+,这意味着空格(空格,制表符或换行符)后跟一个或多个空格。这实际上意味着用一个空格替换两个或更多个空格。

您想要的是用一个空格替换一个或多个空格,因此您可以使用模式\s\s*\s+(推荐)

答案 1 :(得分:62)

<?php
$str = "This is  a string       with
spaces, tabs and newlines present";

$stripped = preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $str);

echo $str;
echo "\n---\n";
echo "$stripped";
?>

此输出

This is  a string   with
spaces, tabs and newlines present
---
This is a string with spaces, tabs and newlines present

答案 2 :(得分:11)

preg_replace('/[\s]+/mu', ' ', $var);

\s已包含标签和新行,因此上面的正则表达式似乎已足够。

答案 3 :(得分:10)

简化为一个功能:

function removeWhiteSpace($text)
{
    $text = preg_replace('/[\t\n\r\0\x0B]/', '', $text);
    $text = preg_replace('/([\s])\1+/', ' ', $text);
    $text = trim($text);
    return $text;
}

基于Danuel O'Neal回答。

答案 4 :(得分:7)

$str='This is   a Text \n and so on Text text.';
print preg_replace("/[[:blank:]]+/"," ",$str);

答案 5 :(得分:7)

我不能在这里复制问题:

$x = "this    \n \t\t \n    works.";
var_dump(preg_replace('/\s\s+/', ' ', $x));
// string(11) "this works."

我不确定它是否只是一个转录错误,但在您的示例中,您使用的是单引号字符串。如果您有双引号字符串,则\n\t仅被视为换行符和制表符。那就是:

'\n\t' != "\n\t"

编辑:正如Codaddict指出的那样,\s\s+不会替换单个制表符。我仍然不认为使用\s+是一个有效的解决方案,所以相反如何:

preg_replace('/(?:\s\s+|\n|\t)/', ' ', $x);

答案 6 :(得分:4)

preg_replace('/(\s\s+|\t|\n)/', ' ', $row['message']);

这将用一个空格替换所有制表符,所有换行符以及多个空格,制表符和换行符的所有组合。

答案 7 :(得分:4)

没有preg_replace()

$str = "This is   a Text \n and so on \t     Text text.";
$str = str_replace(["\r", "\n", "\t"], " ", $str);
while (strpos($str, "  ") !== false)
{
    $str = str_replace("  ", " ", $str);
}
echo $str;

答案 8 :(得分:3)

<?php
#This should help some newbies
# REGEX NOTES FROM DANUEL
# I wrote these functions for my own php framework
# Feel Free to make it better
# If it gets more complicated than this. You need to do more software engineering/logic.
# (.)  // capture any character
# \1   // if it is followed by itself
# +    // one or more

class whitespace{

    static function remove_doublewhitespace($s = null){
           return  $ret = preg_replace('/([\s])\1+/', ' ', $s);
    }

    static function remove_whitespace($s = null){
           return $ret = preg_replace('/[\s]+/', '', $s );
    }

    static function remove_whitespace_feed( $s = null){
           return $ret = preg_replace('/[\t\n\r\0\x0B]/', '', $s);
    }

    static function smart_clean($s = null){
           return $ret = trim( self::remove_doublewhitespace( self::remove_whitespace_feed($s) ) );
    }
}
$string = " Hey   yo, what's \t\n\tthe sc\r\nen\n\tario! \n";
echo whitespace::smart_clean($string);

答案 9 :(得分:1)

您只需按以下方式运行它:

echo preg_replace('/\s{2,}/', ' ', "This is   a Text \n and so on \t     Text text."); // This is a Text and so on Text text.

答案 10 :(得分:1)

这就是我要用的:

一个。请务必使用双引号,例如:

$row['message'] = "This is   a Text \n and so on \t     Text text.";

湾要删除多余的空格,请使用:

$ro = preg_replace('/\s+/', ' ', $row['message']); 
echo $ro;

它可能不是最快的解决方案,但我认为它将需要最少的代码,它应该工作。我从来没用过mysql,所以我可能错了。

答案 11 :(得分:1)

这将使用单个标签替换多个标签

preg_replace("/\s{2,}/", "\t", $string);

答案 12 :(得分:0)

我使用此代码和模式:

preg_replace('/\\s+/', ' ',$data)

$data = 'This is   a Text 
   and so on         Text text on multiple lines and with        whitespaces';
$data= preg_replace('/\\s+/', ' ',$data);
echo $data;

您可以在http://writecodeonline.com/php/

上对此进行测试

答案 13 :(得分:0)

事实上,如果认为你想要这样的东西:

preg_replace('/\n+|\t+|\s+/',' ',$string);

答案 14 :(得分:0)

这里有两种方法:

  1. str_replace(' ', '', $var);
  2. trim($var);

您可以自定义 trim 函数!

答案 15 :(得分:-2)

没有preg_replace,借助循环。

<?php

$str = "This is   a Text \n and so on \t     Text text.";
$str_length = strlen($str);
$str_arr = str_split($str);
for ($i = 0; $i < $str_length; $i++) {
    if (isset($str_arr[$i + 1])
       && $str_arr[$i] == ' '
       && $str_arr[$i] == $str_arr[$i + 1]) {
       unset($str_arr[$i]);
    } 
    else {
      continue;
    }
}

 echo implode("", $str_arr) ; 

 ?>
相关问题