Re: reduce all spaces to one [message #176905 is a reply to message #176904] |
Sat, 04 February 2012 15:05 |
Thomas 'PointedEars'
Messages: 701 Registered: October 2010
Karma:
|
Senior Member |
|
|
Jerry Stuckle wrote:
> On 2/4/2012 9:41 AM, Thomas 'PointedEars' Lahn wrote:
>> 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.
>
> Newline is preserved in my version also,
No, it is not.
$ php -a
Interactive mode enabled
<?php
echo "|foo \n bar|";
echo preg_replace('/\s+/', ' ', "|foo \n bar|");
echo preg_replace('/ +/', ' ', "|foo \n bar|");
|foo
bar||foo bar||foo
bar|
> if you understood ANYTHING about how PHP's regex's work (but we already
> know you don't).
Stop talking to the mirror.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
|
|
|