Re: Re: split on whitespace, preserving whitespace... (a rephrase of the question)

From: Date: Fri, 20 Jul 2001 14:40:56 +0000
Subject: Re: Re: split on whitespace, preserving whitespace... (a rephrase of the question)
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-58908@lists.php.net to get a copy of this message
I thought, Jack's solution should work (with php 4.05 and above), but however if you say it doesn't then there is another solution. I don't find it very hmm "good", but it should work (I cannot test it, because a cant rund php on this machine): $text="your space containing text"; $splittext = new Array(); while ($text){ preg_match("/[^\s]+",$text); if ($machtes[1]){$splittext[]=$matches[1];} preg_replace("/$matches[1]/","",$text); preg_match("/[\s]+",$text); if ($machtes[1]){$splittext[]=$matches[1];} preg_replace("/$matches[1]/","",$text); } If it doesn't work, I'm sorry, but currently I'm not able to test it. But I hope, you can see the idea behind it. best regards Stefan Rusterholz, scripting@interaktion.ch ----- Original Message ----- From: "Garth Dahlstrom" <garth@utmail.to> To: <scripting@interaktion.ch> Cc: <php-general@lists.php.net> Sent: Friday, July 20, 2001 4:13 PM Subject: Re: [PHP] Re: split on whitespace, preserving whitespace... (a rephrase of the question) > On Fri, 20 Jul 2001 15:34:48 +0200 "Stefan Rusterholz" wrote: > > > If you'r using PHP >4 use preg_split. (works also with php 3 >= 3.09) > > see http://www.php.net/manual/en/function.preg-split.php > > for detailed > > information. > > The split pattern you'd need is: > > $sometext2split = "hello world with a lot of whitespaces!"; > > $myarray = preg_split ("/\s+/",$sometext2split); > > > > you should get > > $myarray := ['hello', 'world', 'with', 'a', > > 'lot', 'of', 'whitespaces!'] > > That's not what I'm after though.... > perhaps I should rephrase the question... > > I want to do a split/explode on: > "hello world with a lot of whitespaces!" > on spaces... in such a manner that when I join/implode > it back together I get: > "hello world with a lot of whitespaces!" > not: > "hello world with a lot of whitespaces missing!" > > I'd be happy if I could even get: > $myarray = ["hello ", "world ","with ","a > ","lot ", > "of ","whitespaces!"] > > 'cause I could spell check based on trimming the entry. > > -Garth > > Northern.CA ===-- > http://www.northern.ca > Canada's Search Engine > > > > > > > But remark: \t (tabs) will also count as whitespace, but not \n and > > \r. See > > docu for more informations > > > > Hope, it helps > > best regards > > Stefan Rusterholz, scripting@interaktion.ch > > > > > > ----- Original Message ----- > > From: "Garth Dahlstrom" <garth@utmail.to> > > To: <james@towntalk.co.uk> > > Cc: <php-general@lists.php.net> > > Sent: Friday, July 20, 2001 1:44 PM > > 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 > > > > > > >

« previous php.general (#58908) next »