how to replace many spaces with one space?

how to replace many spaces with one space?

am 25.10.2009 16:21:20 von Jeffry Lunggot

Hi,

how to replace many spaces in any string of character with one space?


Regrads

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: how to replace many spaces with one space?

am 25.10.2009 16:59:56 von David Otton

2009/10/25 Jeffry Lunggot :
> Hi,
>
> how to replace many spaces in any string of character with =A0one space?

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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: how to replace many spaces with one space?

am 25.10.2009 17:28:20 von Ashley Sheridan

--=-kAehbJoNCfcUiRHFQJRl
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Sun, 2009-10-25 at 16:59 +0100, David Otton wrote:

> 2009/10/25 Jeffry Lunggot :
> > Hi,
> >
> > how to replace many spaces in any string of character with one space?
>
> $str = preg_replace('/\s\s+/', ' ', $str);
>


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

You don't need both space characters, as the + symbol matches one or
more, which is just what you want.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-kAehbJoNCfcUiRHFQJRl--

Re: how to replace many spaces with one space?

am 25.10.2009 20:59:58 von Al

Jeffry Lunggot wrote:
> Hi,
>
> how to replace many spaces in any string of character with one space?
>
>
> Regrads


Be careful about the word "spaces" do you mean exactly " "; or white spaces
defined by "\s", which include spaces, tabs CRLFs.

If you want specifically spaces and not all white spaces, then use "\x20"

reg_replace("%\x20+%", " ", $str);//note the space between the quotes in the
replace.

If our string has CRLFs [i.e., newlines] then they will all will be reduced to
one. e.g., " \n\n\n" will end up as " \n" if you use \s.

Also take into account the pattern modifier "m" (PCRE_MULTILINE). It may apply
in your case.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php