Skip to content

Instantly share code, notes, and snippets.

@UziTech
Last active August 3, 2018 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UziTech/8877a79ebffe8b3de9a2 to your computer and use it in GitHub Desktop.
Save UziTech/8877a79ebffe8b3de9a2 to your computer and use it in GitHub Desktop.
Get search terms
/**
* DWTFYW License
*
* Author: Tony Brix, http://tonybrix.info
*
* Example:
* positive terms -----------V-----------V---------V-------V-----------------------V
* console.log(getSearchTerms("this -is 'a string' '-with' positive -'and negative' terms"));
* negative terms ----------------^-------------------------------------^
* Result:
* {
* "positive": [
* "this",
* "a string",
* "-with",
* "positive"
* "terms"
* ],
* "negative": [
* "is",
* "and negative"
* ]
* }
*
* Get search terms separated by spaces from a search query. Negative terms have a dash(-) in front. Terms can have space or dash if quoted.
* @param {string} search Terms
* @return {object} An array of the terms seperated by positive and negative
*/
function getSearchTerms(search) {
const matches = search.match(/-?"[^"]+"|-?'[^']+'|\S+/g);
// sort the terms
const terms = {
positive: [],
negative: []
};
if (matches !== null) {
for (let i = 0; i < matches.length; i++) {
let match = matches[i];
const negative = ("-" === match.substr(0, 1));
if (negative) {
match = match.substr(1);
}
if ((match.substr(0, 1) === "\"" && match.substr(-1) === "\"") || (match.substr(0, 1) === "'" && match.substr(-1) === "'")) {
match = match.substr(1, match.length - 2);
}
if (negative) {
terms.negative.push(match);
} else {
terms.positive.push(match);
}
}
}
return terms;
}
<?php
/**
* DWTFYW License
*
* Author: Tony Brix, http://tonybrix.info
*
* Example:
* positive terms --------V-----------V---------V-------------------------------V
* var_dump(getSearchTerms("this -is 'a string' '-with' -'positive and negative' terms"));
* negative terms -------------^--------------------------------^
* Result:
* array(2) {
* ["positive"]=>
* array(4) {
* [0]=>
* string(4) "this"
* [1]=>
* string(8) "a string"
* [2]=>
* string(5) "-with"
* [3]=>
* string(5) "terms"
* }
* ["negative"]=>
* array(2) {
* [0]=>
* string(2) "is"
* [1]=>
* string(21) "positive and negative"
* }
* }
*
* Get search terms separated by spaces from a search query. Negative terms have a dash(-) in front. Terms can have space or dash if quoted.
* @param string $search
* @return array An array of the terms seperated by positive and negative
*/
function getSearchTerms($search) {
$matches = null;
preg_match_all("/-?\"[^\"]+\"|-?'[^']+'|\S+/", $search, $matches);
// sort the terms
$terms = [
"positive" => [],
"negative" => []
];
foreach ($matches[0] as $match) {
$negative = ("-" === $match[0]);
if ($negative) {
$match = substr($match, 1);
}
if (($match[0] === '"' && substr($match, -1) === '"') || ($match[0] === "'" && substr($match, -1) === "'")) {
$match = substr($match, 1, strlen($match) - 2);
}
if ($negative) {
$terms["negative"][] = $match;
} else {
$terms["positive"][] = $match;
}
}
return $terms;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment