Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #4718

Re: reduce all spaces to one

Message-ID <3604821.WhU2xvQL5g@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-02-04 15:41 +0100
Subject Re: reduce all spaces to one
Newsgroups comp.lang.php
References <jghb1o$62h$1@speranza.aioe.org> <jghd0m$tvr$1@dont-email.me> <jghen802gga@news1.newsguy.com> <9p2tqsF5llU1@mid.uni-berlin.de> <jghhqd$nh0$1@speranza.aioe.org>
Followup-To comp.lang.php

Followups directed to: comp.lang.php

Show all headers | View raw


John wrote:

> Am 03.02.2012 21:19, schrieb M. Strobel:
>> Solution shootout:
>> strobel@s114-intel:~>  php -a
>> Interactive shell
>>
>> php>  echo str_replace('  ',' ','here   are  some      spaces  ');
>> here  are some   spaces
>> php>  echo preg_replace('/\s+/', ' ', 'here   are  some      spaces  ');
>> here are some spaces
>> php>
>>
>> The regex solution is the winner.
> 
> OK.  Thanks to all those who answered !!

It should be noted that if your question was understood literally, the 
solutions presented so far would be wrong.  \s would match too many 
different characters, as it stands for *white-space* in PCRE, _not_ only the 
space character.  In order to reduce only all consecutive *space* characters 
to one space character, you need to write

  echo preg_replace('/ +/', ' ', "here   are  some  \n   spaces  ");

Note that the newline, which is white-space too, is preserved here.

If you want to make the space character in the expression better visible, 
you can use

  (1) echo preg_replace('/\ +/', ' ', "here   are  some  \n   spaces  ");

or

  (2) echo preg_replace('/\\ +/', ' ', "here   are  some  \n   spaces  ");

or

  (3) echo preg_replace('/\x20+/', ' ', "here   are  some  \n   spaces  ");

or

  (4) echo preg_replace('/\\x20+/', ' ', "here   are  some  \n   spaces  ");

Your approach,

      echo preg_replace('/[ ]+/', ' ', "here   are  some  \n   spaces  ");

is equivalent to that, but slightly less efficient because of the character 
class (even in Visual Basic .NET).  However, it also has the advantage over 
the simple solutions (1) and (2) that multiple spaces in the character class 
will still only match one space.


PointedEars
-- 
> If you get a bunch of authors […] that state the same "best practices"
> in any programming language, then you can bet who is wrong or right...
Not with javascript. Nonsense propagates like wildfire in this field.
  -- Richard Cornford, comp.lang.javascript, 2011-11-14

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

reduce all spaces to one John <john_takethisoff@agdp.de> - 2012-02-03 20:03 +0100
  Re: reduce all spaces to one Jerry Stuckle <jstucklex@attglobal.net> - 2012-02-03 14:36 -0500
    Re: reduce all spaces to one crankypuss <no@email.thanks> - 2012-02-03 13:05 -0700
      Re: reduce all spaces to one Jerry Stuckle <jstucklex@attglobal.net> - 2012-02-03 15:17 -0500
      Re: reduce all spaces to one "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-02-03 21:19 +0100
        Re: reduce all spaces to one John <john_takethisoff@agdp.de> - 2012-02-03 21:59 +0100
          Re: reduce all spaces to one Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-04 15:41 +0100
            Re: reduce all spaces to one Jerry Stuckle <jstucklex@attglobal.net> - 2012-02-04 09:57 -0500
              Re: reduce all spaces to one Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-04 16:05 +0100
              Re: reduce all spaces to one Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-04 16:21 +0100
                Re: reduce all spaces to one Jerry Stuckle <jstucklex@attglobal.net> - 2012-02-04 16:00 -0500
              Re: reduce all spaces to one Michael Fesser <netizen@gmx.de> - 2012-02-05 21:47 +0100
                Re: reduce all spaces to one Jerry Stuckle <jstucklex@attglobal.net> - 2012-02-05 16:12 -0500
                Re: reduce all spaces to one "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-02-05 22:23 +0100
                Re: reduce all spaces to one The Natural Philosopher <tnp@invalid.invalid> - 2012-02-05 21:37 +0000
                Re: reduce all spaces to one Michael Fesser <netizen@gmx.de> - 2012-02-05 23:34 +0100
            Re: reduce all spaces to one John <john_takethisoff@agdp.de> - 2012-02-04 22:26 +0100
              Re: reduce all spaces to one Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-05 00:05 +0100
                Re: reduce all spaces to one John <john_takethisoff@agdp.de> - 2012-02-05 01:39 +0100
                Re: reduce all spaces to one Jerry Stuckle <jstucklex@attglobal.net> - 2012-02-04 20:56 -0500
                Re: reduce all spaces to one John <john_takethisoff@agdp.de> - 2012-02-05 18:02 +0100
                Re: reduce all spaces to one Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-05 03:15 +0100
                Re: reduce all spaces to one John <john_takethisoff@agdp.de> - 2012-02-05 18:04 +0100
                Re: reduce all spaces to one Doug Miller <doug_at_milmac_dot_com@example.com> - 2012-02-05 04:02 +0000
                Re: reduce all spaces to one Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-05 05:35 +0100
                Re: reduce all spaces to one Doug Miller <doug_at_milmac_dot_com@example.com> - 2012-02-06 01:31 +0000
                Re: reduce all spaces to one "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-02-06 09:31 +0100
                [OT]: "native" English (was: reduce all spaces to one) Gregor Kofler <usenet@gregorkofler.com> - 2012-02-05 11:03 +0100
                Re: [OT]: "native" English The Natural Philosopher <tnp@invalid.invalid> - 2012-02-05 10:11 +0000
                Re: [OT]: "native" English Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-05 14:00 +0100
                Re: [OT]: "native" English The Natural Philosopher <tnp@invalid.invalid> - 2012-02-05 13:53 +0000
                Re: reduce all spaces to one John <john_takethisoff@agdp.de> - 2012-02-05 18:05 +0100
  Re: reduce all spaces to one "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-02-03 21:30 +0100

csiph-web