RE: [PHP] RegExp for preg_split()
| From: | Weber Sites LTD | Date: | Sat, 29 Apr 2006 17:30:27 +0000 |
| Subject: | RE: [PHP] RegExp for preg_split() | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-235101@lists.php.net to get a copy of this message | ||
Thanks,
But this example seems to be short and does the job :
<?php
$String='this is a "test expression" for search input';
$MyRegEx = '/"[^"]+"|[^"\s,]+/';
preg_match_all($MyRegEx, $String, $Tokens);
?>
Unless I'm missing something?
thanks
-----Original Message-----
From: Richard Lynch [mailto:ceo@l-i-e.com]
Sent: Saturday, April 29, 2006 10:29 AM
To: Weber Sites LTD
Cc: php-general@lists.php.net
Subject: Re: [PHP] RegExp for preg_split()
On Fri, April 28, 2006 11:16 am, Weber Sites LTD wrote:
> I'm looking for the RegExp that will split a search string into search
> keywords.
> while taking " " into account.
>
> From what I managed to find I can get all of the words into an array
> but I would like all of the words inside " " to be in the same array
> cell.
I'd be pretty surprised if searching for things like:
PHP simple search input quotes
and things like that didn't turn up some solutions...
You might have to search in specific forums rather than a general Google,
but.
Here's one crude solution though:
<?php
$input = 'this is a "test expression" for search input'; //remove duplicate
spaces:
$input = preg_replace('/\\s+/', ' ', $input); //ignore leading/trailing
blanks:
$input = trim($input);
$parts = explode('"', $input);
$terms = array();
$in_quotes = false;
foreach($parts as $expression){
$expression = trim($expression); //probably not needed...
if (strlen($expression)){
if (!$in_quotes){
//individual words:
$words = explode(' ', $expression);
$terms = array_merge($terms, $words);
}
else{
//in quotes, so this is a search term:
$terms[] = $expression;
}
}
$in_quotes = !$in_quotes;
}
var_dump($terms);
?>
Note that invalid input such as unbalanced quote marks will mess this up
big-time, probably...
But maybe that's just as well...
--
Like Music?
http://l-i-e.com/artists.htm
--
PHP General Mailing List (http://www.php.net/) To unsubscribe, visit:
http://www.php.net/unsub.php