note 47568 deleted from function.explode by bjori
| From: | bjori@php.net | Date: | Mon, 25 Sep 2006 08:11:54 +0000 |
| Subject: | note 47568 deleted from function.explode by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-117599@lists.php.net to get a copy of this message | ||
Note Submitter: corychristison }at{ lavacube (DOT) com
----
Here is a small script I use to break a part search queries[example:
"do-search"-"dont-search" ]
+ or no 'switch' is to add to search tearms
- for do not include in search
<?php
function parse_search ($input) {
$c = count_chars($input, 0);
$c = ($c[34]/ 2);
if(!strstr($c, ".") && $c != "0"){
$input = explode("\"", $input);
$include = array();
$exclude = array();
$switches = array("+", "-");
for($i=0; $i < count($input); $i++){
$inst = $input[$i];
if($inst == "" && ($i == "0" || $i == (count($input) - 1)) ){
$inst = "+"; }
if(in_array($inst, $switches)){
$lswitch = $inst;
}else{
if($inst != ""){
if($lswitch == "-"){
$exclude[] = $inst;
}elseif($lswitch == "+"){
$include[] = $inst;
}else{
$include[] = $inst;
}
}
unset($lswitch);
}
} // end loop
$output = array("include" => $include, "exclude" => $exclude);
}else{
$output = array("include" => explode(" ", trim($input)));
}
return $output;
} // end function
?>
Sorry for my somewhat messy and hard to follow code.
An example of the code would be:
<?php
$search = '"isearch"-"this"'
$do_search = parse_search($search);
print_r($do_search);
?>
will output:
Array
(
[include] => Array
(
[0] => isearch
)
[exclude] => Array
(
[0] => this
)
)