0 || $theSpace > 0) { if ($thePlus > 0 && ($theSpace == 0 || $thePlus < $theSpace)) { $wordLength = $thePlus - $position; $mandatoryToggle = 1; $lastPosition = $thePlus + 1; } elseif ($theSpace > 0 && ($thePlus == 0 || $theSpace < $thePlus)) { $wordLength = $theSpace - $position; $lastPosition = $theSpace + 1; } // Get the word, increase word count $keywords[0][$keywordCount] = substr($string,$position,$wordLength); $keywordCount++; $keywords[1][$keywordCount] = $mandatoryToggle; // the state applies to the next word $position = $lastPosition; // increase position inside the string } else { $position = $length; // nothing has been found. Set position to last character to break the loop } } while ($position < $length); // Get last (or only) word in list $keywords[0][$keywordCount] = substr($string,$lastPosition,$length - $lastPosition + 1); /* * Build the part of the SQL statement corresponding to the comparison of the * field name to the keywords, with the list of words and the appropriate logical operators */ $sqlString = ""; if ($insensitive) { $operator = "ILIKE"; } else { $operator = "LIKE"; } for ($i = 0; $i <= $keywordCount; $i++) { $sqlString .= "\"$fieldName\" $operator '%{$keywords[0][$i]}%'"; /* * Logical operators are set only if there is more than one word. * The toggle to consider is the one from the next word * (or always AND is $allKeywords is true) */ if ($i < $keywordCount) { if ($allKeywords) { $sqlString .= " AND "; } else { if ($keywords[1][$i+1] == 1) { $sqlString .= " AND "; } else { $sqlString .= " OR "; } } } } return $sqlString; } ?>