Re: [PHP] Re: split on whitespace, preserving whitespace...
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 witha lot ofwhitespaces!; $myarray = preg_split (/\s+/,$sometext2split); you should get $myarray := ['hello', 'world', 'with', 'a', 'lot', 'of', 'whitespaces!'] 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, [EMAIL PROTECTED] - Original Message - From: Garth Dahlstrom [EMAIL PROTECTED] To: [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] 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 On Fri, 20 Jul 2001 09:20:59 +0100 James Holloway wrote: try $wordarr = explode( , $string); James. - Original Message - From: Garth Dahlstrom [EMAIL PROTECTED] Newsgroups: php.general To: [EMAIL PROTECTED] 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 contanswhite 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]),'.,/')) { file://check spelling file://correct errors } echo $wordarr[$i]; } and end up with: This containswhite 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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Re: split on whitespace, preserving whitespace...
$text = This contanswhite space .; $matches = preg_split(/(\s+)/,$text,-1, PREG_SPLIT_DELIM_CAPTURE); echo implode('',$matches); try that jack -Original Message- From: Garth Dahlstrom [mailto:[EMAIL PROTECTED]] Sent: Friday, July 20, 2001 7:45 AM To: [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] 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 [EMAIL PROTECTED] Newsgroups: php.general To: [EMAIL PROTECTED] 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 contanswhite 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 containswhite 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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Re: split on whitespace, preserving whitespace...
Uuhmm, I'm sorry. I haven't read it throught to the end. The solution ist a bit different: use preg_replace to replace multiple spaces with single spaces: $sometext2split = preg_replace(|, ,$sometext2split); #make shure that the separator isn't in the text anywhere around $sometext2split = preg_replace(/\s+/, | |,$sometext2split); and then explode the string (i don't know wether explode or preg_split is faster, I tend to explode for simple split operations): $myarray = explode(|,$sometext2split); this should work. But probably it was a better solution to insert the spaces with a loop between the words than to run the risk, that the separator is somewhere in the text... best regards Stefan Rusterholz, [EMAIL PROTECTED] - Original Message - From: Stefan Rusterholz [EMAIL PROTECTED] To: [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Friday, July 20, 2001 3:34 PM Subject: Re: [PHP] Re: split on whitespace, preserving whitespace... 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 witha lot ofwhitespaces!; $myarray = preg_split (/\s+/,$sometext2split); you should get $myarray := ['hello', 'world', 'with', 'a', 'lot', 'of', 'whitespaces!'] 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, [EMAIL PROTECTED] - Original Message - From: Garth Dahlstrom [EMAIL PROTECTED] To: [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] 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 On Fri, 20 Jul 2001 09:20:59 +0100 James Holloway wrote: try $wordarr = explode( , $string); James. - Original Message - From: Garth Dahlstrom [EMAIL PROTECTED] Newsgroups: php.general To: [EMAIL PROTECTED] 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 contanswhite 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]),'.,/')) { file://check spelling file://correct errors } echo $wordarr[$i]; } and end up with: This containswhite 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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Re: split on whitespace, preserving whitespace...
Several of these solutions are good ideas, but they don't preserve the whitespace as he desired. Preg_split with the capturing of the (\s+) delimiter does the trick. Jack -Original Message- From: Stefan Rusterholz [mailto:[EMAIL PROTECTED]] Sent: Friday, July 20, 2001 9:48 AM To: [EMAIL PROTECTED] Subject: Re: [PHP] Re: split on whitespace, preserving whitespace... Uuhmm, I'm sorry. I haven't read it throught to the end. The solution ist a bit different: use preg_replace to replace multiple spaces with single spaces: $sometext2split = preg_replace(|, ,$sometext2split); #make shure that the separator isn't in the text anywhere around $sometext2split = preg_replace(/\s+/, | |,$sometext2split); and then explode the string (i don't know wether explode or preg_split is faster, I tend to explode for simple split operations): $myarray = explode(|,$sometext2split); this should work. But probably it was a better solution to insert the spaces with a loop between the words than to run the risk, that the separator is somewhere in the text... best regards Stefan Rusterholz, [EMAIL PROTECTED] - Original Message - From: Stefan Rusterholz [EMAIL PROTECTED] To: [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Friday, July 20, 2001 3:34 PM Subject: Re: [PHP] Re: split on whitespace, preserving whitespace... 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 witha lot ofwhitespaces!; $myarray = preg_split (/\s+/,$sometext2split); you should get $myarray := ['hello', 'world', 'with', 'a', 'lot', 'of', 'whitespaces!'] 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, [EMAIL PROTECTED] - Original Message - From: Garth Dahlstrom [EMAIL PROTECTED] To: [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] 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 On Fri, 20 Jul 2001 09:20:59 +0100 James Holloway wrote: try $wordarr = explode( , $string); James. - Original Message - From: Garth Dahlstrom [EMAIL PROTECTED] Newsgroups: php.general To: [EMAIL PROTECTED] 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 contanswhite 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]),'.,/')) { file://check spelling file://correct errors } echo $wordarr[$i]; } and end up with: This containswhite 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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net
Re: [PHP] Re: split on whitespace, preserving whitespace...
And after having read it thorugh I remark, that I'm going to disgrace myself... :-( Just forget my answer, Jack Dempsey is right. Sorry - Original Message - From: Stefan Rusterholz [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Friday, July 20, 2001 3:47 PM Subject: Re: [PHP] Re: split on whitespace, preserving whitespace... Uuhmm, I'm sorry. I haven't read it throught to the end. The solution ist a bit different: use preg_replace to replace multiple spaces with single spaces: $sometext2split = preg_replace(|, ,$sometext2split); #make shure that the separator isn't in the text anywhere around $sometext2split = preg_replace(/\s+/, | |,$sometext2split); and then explode the string (i don't know wether explode or preg_split is faster, I tend to explode for simple split operations): $myarray = explode(|,$sometext2split); this should work. But probably it was a better solution to insert the spaces with a loop between the words than to run the risk, that the separator is somewhere in the text... best regards Stefan Rusterholz, [EMAIL PROTECTED] - Original Message - From: Stefan Rusterholz [EMAIL PROTECTED] To: [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Friday, July 20, 2001 3:34 PM Subject: Re: [PHP] Re: split on whitespace, preserving whitespace... 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 witha lot ofwhitespaces!; $myarray = preg_split (/\s+/,$sometext2split); you should get $myarray := ['hello', 'world', 'with', 'a', 'lot', 'of', 'whitespaces!'] 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, [EMAIL PROTECTED] - Original Message - From: Garth Dahlstrom [EMAIL PROTECTED] To: [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] 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 On Fri, 20 Jul 2001 09:20:59 +0100 James Holloway wrote: try $wordarr = explode( , $string); James. - Original Message - From: Garth Dahlstrom [EMAIL PROTECTED] Newsgroups: php.general To: [EMAIL PROTECTED] 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 contanswhite 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]),'.,/')) { file://check spelling file://correct errors } echo $wordarr[$i]; } and end up with: This containswhite 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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL
Re: [PHP] Re: split on whitespace, preserving whitespace...
On Fri, 20 Jul 2001 09:32:00 -0400 Jack Dempsey wrote: $text = This contanswhite space .; $matches = preg_split(/(\s+)/,$text,-1, PREG_SPLIT_DELIM_CAPTURE); echo implode('',$matches); Nope that doesn't do it. here's the test: echo pre[$text]br[. implode('',$matches).]/pre; here's the output: [This contanswhite space .] [Thiscontanswhitespace.] If it were working the two lines should be identical. Northern.CA ===-- http://www.northern.ca Canada's Search Engine -Original Message- 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. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
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 witha lot ofwhitespaces!; $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 witha lot ofwhitespaces! on spaces... in such a manner that when I join/implode it back together I get: hello world witha lot ofwhitespaces! 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, [EMAIL PROTECTED] - Original Message - From: Garth Dahlstrom [EMAIL PROTECTED] To: [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Re: split on whitespace, preserving whitespace...
The workstation I'm on is only running 4.0.4... perhaps it's time to upgrade. :) I'll try it again. On Fri, 20 Jul 2001 10:30:18 -0400 Jack Dempsey wrote: Actually, it does. I just ran it again with that exact code. What version of php are you running? Here's a direct copy/paste of what I get: code ? $text = This contanswhite space .; $matches = preg_split(/(\s+)/,$text,-1, PREG_SPLIT_DELIM_CAPTURE); echo pre[$text]br[. implode('',$matches).]/pre; ? /code ouput pre[This contanswhite space .]br[This contanswhite space .]/pre /output Are you running php = 4.0.5? jack -Original Message- From: Garth Dahlstrom [mailto:[EMAIL PROTECTED]] Sent: Friday, July 20, 2001 10:06 AM To: [EMAIL PROTECTED] Cc: [EMAIL PROTECTED]; [EMAIL PROTECTED] Subject: Re: [PHP] Re: split on whitespace, preserving whitespace... On Fri, 20 Jul 2001 09:32:00 -0400 Jack Dempsey wrote: $text = This contanswhite space .; $matches = preg_split(/(\s+)/,$text,-1, PREG_SPLIT_DELIM_CAPTURE); echo implode('',$matches); Nope that doesn't do it. here's the test: echo pre[$text]br[. implode('',$matches).]/pre; here's the output: [This contanswhite space .] [Thiscontanswhitespace.] If it were working the two lines should be identical. Northern.CA ===-- http://www.northern.ca Canada's Search Engine -Original Message- 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. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] Northern.CA ===-- http://www.northern.ca Canada's Search Engine -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Re: split on whitespace, preserving whitespace... (a rephrase of the question)
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, [EMAIL PROTECTED] - Original Message - From: Garth Dahlstrom [EMAIL PROTECTED] To: [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] 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 witha lot ofwhitespaces!; $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 witha lot ofwhitespaces! on spaces... in such a manner that when I join/implode it back together I get: hello world witha lot ofwhitespaces! 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, [EMAIL PROTECTED] - Original Message - From: Garth Dahlstrom [EMAIL PROTECTED] To: [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Re: split on whitespace, preserving whitespace... (a rephrase of the question)
In article [EMAIL PROTECTED], [EMAIL PROTECTED] (Garth Dahlstrom) wrote: $sometext2split = hello world witha lot ofwhitespaces!; $myarray = preg_split (/\s+/,$sometext2split); you should get $myarray := ['hello', 'world', 'with', 'a', 'lot', 'of', 'whitespaces!'] That's not what I'm after though snip 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. Maybe you've already tried this and rejected this for some reason, but my inclination would be to split on \b (the non-character boundary between a \s+ next to a \w+). -- CC -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]