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


Groups > comp.lang.php > #4724

Re: reduce all spaces to one

Message-ID <1669543.FqOfKyIYee@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-02-05 00:05 +0100
Subject Re: reduce all spaces to one
Newsgroups comp.lang.php
References (2 earlier) <jghen802gga@news1.newsguy.com> <9p2tqsF5llU1@mid.uni-berlin.de> <jghhqd$nh0$1@speranza.aioe.org> <3604821.WhU2xvQL5g@PointedEars.de> <jgk7qi$rgb$1@speranza.aioe.org>
Followup-To comp.lang.php

Followups directed to: comp.lang.php

Show all headers | View raw


John wrote:

> Am 04.02.2012 15:41, schrieb Thomas 'PointedEars' Lahn:
                       ^^^^^^^
>> 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.
> 
> in fact, just by 'accident' the 'regex' solutions fits perfectly in what
> I have to do next, i.e. feed the text through an 'explode' statement,
> which takes ' ' as separator.

I do not think you have fully understood what I have said (maybe you want to 
try posting to *de*.comp.lang.php instead?).  *All* presented solutions so 
far, including yours and mine, are "'regex' solutions".  But the set of 
characters that are replaced differs between them.

Anyhow, you do not need to replace spaces before word splitting.  Instead of 
PHP 5+ [1]

  $words = str_split(' ', preg_replace('/\\s+/', ' ', $text));

since PHP 4 you can just split at consecutive white-space [2]:

  $words = preg_split('/\\s+/', $text);

Also note that for finding the *words* in a text, this splitting at white-
space is _not_ sufficient.  For example, in the sentence before splitting at 
white-space would have resulted in words "text," "_not_", and "sufficient.".  
Once you use preg_split(), though, there is a simple remedy (which can be 
found at [2]).  Just include all characters that you do not want to be part 
of a word in the character class:

  $words = preg_split('/[\\s,]+/', $text);

(I prefer to write `\\' instead of `\' even within single-quoted strings, in 
order to make the `\' explicit.  YMMV.)

You might also want to exclude periods (`.') and other punctuation from 
words, like dashes and ellipses.  Note that PCRE provides an escape sequence 
for ASCII-non-word characters [3], and that it supports Unicode character 
properties, which can be used to differentiate between letters and non-
letters in various natural languages [4].

You might find [5] useful, in particular [6], for the next time that you 
post (translations are available there).


PointedEars
___________
[1] <http://php.net/str_split>
[2] <http://php.net/preg_split>
[3] <http://php.net/manual/en/regexp.reference.escape.php>
[4] <http://php.net/manual/en/regexp.reference.unicode.php>
[5] <http://catb.org/esr/faqs/smart-questions.html>
[6] <http://catb.org/esr/faqs/smart-questions.html#goal>
-- 
    realism:    HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness:    XHTML 1.1 as application/xhtml+xml
                                                    -- Bjoern Hoehrmann

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