Re: split on whitespace, preserving whitespace...
| From: | Garth Dahlstrom | Date: | Fri, 20 Jul 2001 20:32:18 +0000 |
| Subject: | Re: split on whitespace, preserving whitespace... | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-58972@lists.php.net to get a copy of this message | ||
Yup, I hear that... I'm going to use something like the following to
do my pspell application...
<pre>
<?PHP
$text = "This contans (brackets) {white} shaz-zam this_under space; .";
$matches = preg_split("/([\.\;\:\!\{\}\(\)\s]+)/",$text,-1,
PREG_SPLIT_DELIM_CAPTURE);
echo "<pre>[$text]<br>[".
implode('|',$matches)."]</pre>";
?>
</pre>
On Fri, 20 Jul 2001 15:11:26 -0500 "Brad S. Jackson" wrote:
>
>
> I got this to work. I wish I had found this when I wrote our pspell
> code. I
> wrote code that loops through each character to get the words and
> preserve white
> space. I also wrote a whole bunch of code that strips punctuation
> from the
> beginning and end of words and has regex checking for numbers, dates
> and times.
>
> $text = "This is a lotta
> text. How do you like that?";
>
> preg_match_all("/(\\S+)|(\\s+)/ms", $text, $array);
>
> echo "<pre>";
> print_r($array[1]);
>
>
> Garth Dahlstrom <garth@utmail.to> wrote:
> 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
>
>
Northern.CA ===--
http://www.northern.ca
Canada's Search Engine