FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » reduce all spaces to one
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: reduce all spaces to one [message #176903 is a reply to message #176902] Sat, 04 February 2012 14:41 Go to previous messageGo to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma:
Senior Member
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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Stats comp.lang.php (last 7 days)
Next Topic: Check email
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 14:22:02 GMT 2024

Total time taken to generate the page: 0.05285 seconds