Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder4.news.weretis.net!newsfeed.straub-nv.de!uucp.gnuu.de!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="ISO-8859-1" Message-ID: <1669543.FqOfKyIYee@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Sun, 05 Feb 2012 00:05:07 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 7Bit Subject: Re: reduce all spaces to one Newsgroups: comp.lang.php References: <9p2tqsF5llU1@mid.uni-berlin.de> <3604821.WhU2xvQL5g@PointedEars.de> Followup-To: comp.lang.php MIME-Version: 1.0 Lines: 67 NNTP-Posting-Date: 05 Feb 2012 00:05:07 CET NNTP-Posting-Host: bba90966.newsspool1.arcor-online.net X-Trace: DXC=J?no:MA2NBo]l@YUW5NBknic==]BZ:afn4Fo<]lROoRa<`=YMgDjhgbjVQdlSRe[boDZm8W4\YJNlb@mF9jNikdeU[[_>Eh^06gVDjJ1FXdf7f X-Complaints-To: usenet-abuse@arcor.de Xref: x330-a1.tempe.blueboxinc.net comp.lang.php:4724 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] [2] [3] [4] [5] [6] -- realism: HTML 4.01 Strict evangelism: XHTML 1.0 Strict madness: XHTML 1.1 as application/xhtml+xml -- Bjoern Hoehrmann