Re: Re: Parse search string a la Google (Regular expression?)

From: Date: Thu, 28 Nov 2002 20:28:15 +0000
Subject: Re: Re: Parse search string a la Google (Regular expression?)
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-126436@lists.php.net to get a copy of this message
Works like a charm, thank you! Benny "Ernest E Vogelsinger" <ernest@vogelsinger.at> wrote in message news:5.1.1.6.2.20021128004129.048915e8@mail.vogelsinger.at... > At 23:11 27.11.2002, Ernest E Vogelsinger said: > --------------------[snip]-------------------- > >If I understand you correctly you want to isolate either quoted strings > >(with or without whitespace), or tokens separated by whitespace, as array > >elements? > > > >For this you would first have to isolate the first quoted sentence, then > >tokenize the part before, and loop this as long you're not done. > > > >Should work something like that: > > > > [...] > >Disclaimer: untested as usual. _Should_ behave like this: > --------------------[snip]-------------------- > > I _should_ have tested. This script actually works the way you expect it to be: > > ------------<code>------------ > <xmp> > <?php > > function tokenize_search($input) { > $re = '/\s*(.*?)\s*"\s*([^"]*?)\s*"\s*(.*)/s'; > /* look for 3 groups: > a - prematch - anything up to the first quote > b - match - anything until the next quote > c - postmatch - rest of the string > */ > $tokens = array(); > while (preg_match($re, $input, $aresult)) { > // aresult contains: [0]-total [1]-a [2]-b [3]-c > // tokenize the prematch > if ($aresult[1]) $tokens = array_merge($tokens, explode(' ', > $aresult[1])); > array_push($tokens, $aresult[2]); > $input = $aresult[3]; > } > // $input has the rest of the line > if ($input) $tokens = array_merge($tokens, explode(' ', $input)); > return $tokens; > } > > $string = "\"search for this sentence\" -NotForThisWord > ButDefinitelyForThisWord"; > $tokens = tokenize_search($string); > print_r($tokens); > > ?> > ------------</code>------------ > > The output of this script is: > > Array ( > [0] => search for this sentence > [1] => -NotForThisWord > [2] => ButDefinitelyForThisWord > ) > > > -- > >O Ernest E. Vogelsinger > (\) ICQ #13394035 > ^ http://www.vogelsinger.at/ > >

« previous php.general (#126436) next »