RE: [PHP] Re: split on whitespace, preserving whitespace...

From: Date: Fri, 20 Jul 2001 13:32:00 +0000
Subject: RE: [PHP] Re: split on whitespace, preserving whitespace...
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-58894@lists.php.net to get a copy of this message
$text = "This contans white space ."; $matches = preg_split("/(\s+)/",$text,-1, PREG_SPLIT_DELIM_CAPTURE); echo implode('',$matches); try that jack -----Original Message----- From: Garth Dahlstrom [mailto:garth@utmail.to] Sent: Friday, July 20, 2001 7:45 AM To: james@towntalk.co.uk Cc: php-general@lists.php.net Subject: [PHP] Re: split on whitespace, preserving whitespace... > try $wordarr = explode(" ", $string); mmm... yeah... That's what I had before (acutally I was using the alias split)... it does NOT however preserve the areas of white space. Instead the target of: > > $wordsarr = ('This',' ','contans',' > > ','white',' ','space',' ','.') You get: > > $wordsarr = > > ('This','','contans','','white','space','','.') which has totally destroyed the whitespace I'm trying to keep intacted. I'm starting to wonder if maybe preg_match_all is better suited for something like this... just no idea how to do the regex that matches words & spaces and populates them into the array. maybe something like: echo preg_match_all("|(\s+)?(.*)?(\s+)?|",$test,$arr); sigh... I dunno regex. -Garth Northern.CA ===-- http://www.northern.ca Canada's Search Engine On Fri, 20 Jul 2001 09:20:59 +0100 "James Holloway" wrote: > try $wordarr = explode(" ", $string); > > James. > > ----- Original Message ----- > From: "Garth Dahlstrom" <garth@utmail.to> > Newsgroups: php.general > To: <php-general@lists.php.net> > Sent: Friday, July 20, 2001 5:43 AM > Subject: split on whitespace, preserving whitespace... > > > > Hi all, > > > > I'm trying to build a spell checker for a web form. > > What has got me stumped is being able to do a split > > so that whitespace and words are stored as seperate > > elements of the same array. > > > > ideally, I'd use some form of preg_split to put: > > > > "This contans white space ." > > > > into an array like: > > > > $wordsarr = ('This',' ','contans',' > > ','white',' ','space',' ','.') > > > > So that I can do a a loop as follows: > > > > for ($i = 0; $i < count($wordarr); $i++) > > { > > if (!trim($wordarr[$i]) == "" && > > !eregi(trim($wordarr[$i]),'.,/')) > > { > > //check spelling > > //correct errors > > } > > echo $wordarr[$i]; > > } > > and end up with: > > "This contains white space ." > > > > can a split like this be accomplished using > > preg_split or do I need to go through the string > > one space at a time in a while loop? > > > > -Garth > > > > Northern.CA ===--- > > http://www.northern.ca > > > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#58894) next »