note 47568 added to function.explode
| From: | (DOT) | Date: | Sat, 20 Nov 2004 19:06:39 +0000 |
| Subject: | note 47568 added to function.explode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-80809@lists.php.net to get a copy of this message | ||
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
)
)
----
Manual Page -- http://www.php.net/manual/en/function.explode.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+47568
Delete -- http://master.php.net/manage/user-notes.php?action=delete+47568&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47568&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47568&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47568&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47568&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47568&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+47568&report=yes
Search -- http://master.php.net/manage/user-notes.php