Ahh, there was more nonsense to be corrected …
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, if you understood ANYTHING
> about how PHP's regex's work (but we already know you don't).
JFYI, with preg_* methods, PHP's Regular Expressions are *Perl-Compatible*
Regular Expressions. That is what the `p' stands for there (as opposed to
ereg_*, which are now [PHP 5.3] deprecated methods using POSIX *E*xtended
Regular Expressions).
>> 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
>> ");
>>
>
> The search pattern is invalid.
Perfectly valid
$ php -v
PHP 5.3.10-1 (cli) (built: Feb 3 2012 10:03:01)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
with XCache v1.3.2, Copyright (c) 2005-2011, by mOo
with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans
with Suhosin v0.9.32.1, Copyright (c) 2007-2010, by SektionEins GmbH
>> or
>>
>> (2) echo preg_replace('/\\ +/', ' ', "here are some \n spaces
>> ");
>>
>
> Searches for the sequence '\ '
Not in the PHP version mentioned above.
>> or
>>
>> (3) echo preg_replace('/\x20+/', ' ', "here are some \n spaces
>> ");
>>
>
> Ugh!
man 3 pcrepattern
>> or
>>
>> (4) echo preg_replace('/\\x20+/', ' ', "here are some \n spaces
>> ");
>>
>
> Again, invalid escape sequence.
Again, perfectly valid and working as intended in the PHP version mentioned
above. Probably valid in *all* PHP versions that support PCREs. You have
not bothered to do any tests before posting, have you?
>> 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.
>
> Showing your ignorance again. You do better when you keep your mouth
> shut.
Pot, kettle, black.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
|