我设法删除了空格,但我不明白为什么它也会删除我的返回。我有一个文本区域在我的形式,我想允许最多两个返回。以下是我到目前为止一直使用的。
$string = preg_replace('/'s's+/', ' ', $string); // supposed to remove more than one consecutive space - but also deletes my returns ...
$string = preg_replace('/'n'n'n+/', ''n'n', $string); // using this one by itself does not do as expected and removes all returns ...
似乎第一行已经摆脱了多个空格和所有返回…这很奇怪。
因为's
也将匹配换行符。因此,我建议您使用'h
来匹配任何类型的水平空格。
$string = preg_replace('/'h'h+/', ' ', $string);
's match any white space character ['r'n't'f ]
参见's
的定义,其中包括'n
。
'h matches any horizontal whitespace character (equal to [[:blank:]])
使用'h
作为水平空白
对于那些需要它的人来说,这就是从文本区域中删除两个回车符的方法。
preg_replace('/'n'r('n'r)+/', "'n'r", $str);
对于空间问题,正如上面发布的那样,将's替换为'h