= ($MAX_TERMS - 1))) { $flagwild = 0; } // find opening special characters while ((substr($term, $countprefix, 1) == "(" || substr($term, $countprefix, 1) == '"' || substr($term, $countprefix, 1) == "+" || substr($term, $countprefix, 1) == "-" || substr($term, $countprefix, 1) == "~" || substr($term, $countprefix, 1) == "<" || substr($term, $countprefix, 1) == ">") && ($countprefix+$countsuffix) < strlen($term)) { if (substr($term, $countprefix, 1) == '"') { $countquote++; $flagwild = 0; } if (substr($term, $countprefix, 1) == "(") { $countparen++; $flagopenparen = 1; } $countprefix++; } // reset flagwild to 1 if we're under MAX_TERMS (only runs if we're not in the middle of quotes, and forced to run if we're still in paren) if ($countquote == 0 && ($countparen > 0 || $numtermsextra < ($MAX_TERMS - 1))) { $flagwild = 1; } // find closing special characters while ((substr($term, -$countsuffix-1, 1) == ")" || substr($term, -$countsuffix-1, 1) == '"') && ($countprefix+$countsuffix) < strlen($term)) { if (substr($term, -$countsuffix-1, 1) == '"') { $countquote = max(0, $countquote-1); } if (substr($term, -$countsuffix-1, 1) == ")") { $countparen = max(0, $countparen-1); $flagcloseparen = 1; } $countsuffix++; } // split term $termprefix = substr($term, 0, $countprefix); $termterm = substr($term."A", $countprefix, -$countsuffix-1); // artificial padded A assures that the third argument is always negative $termsuffix = substr($term, -$countsuffix, $countsuffix); // add extra terms with wildcards if ($flagwild == 1 && substr($termterm, -1, 1) != "*" && strlen($termterm) > 0) { // @todo: make this i18n friendly with the plural character (only works here with s) $termsextra = $termsextra . $termprefix . $prefix . rtrim($termterm, "s") . "*" . $termsuffix . " "; $numtermsextra++; } elseif ($flagopenparen == 1 && $flagcloseparen == 0) { $termsextra = $termsextra . str_replace('"', '', $termprefix); } elseif ($flagopenparen == 0 && $flagcloseparen == 1) { $termsextra = preg_replace('/\s+$/', '', $termsextra) . ") "; } // add short search prefixes if (strlen($termterm) > 0) { $term = $termprefix . $prefix . $termterm . $termsuffix; } } // implode terms, trim termsextra trailing space (if it exists) $terms = implode(" ", $terms); $termsextra = preg_replace('/\s+$/', '', $termsextra); // add extra closing quotes and parentheses while ($countquote > 0) { $terms = $terms.'"'; $termsextra = $termsextra.'"'; $countquote--; } while ($countparen > 0) { $terms = $terms.")"; $termsextra = $termsextra.")"; $countparen--; } // all done! return ($terms." ".$termsextra); } }