2

Is there an easy way to parse a string for search terms including negative terms?

'this -that "the other thing" -"but not this" "-positive"' 

would change to

array(
  "positive" => array(
    "this",
    "the other thing",
    "-positive"
  ),
  "negative" => array(
    "that",
    "but not this"
  )
)

so those terms could be used to search.

2
  • What you want to change exactly? I don't understand what you have and what you want to change?!? Please show how the array looks at the start and how it should look at the end! Commented Oct 31, 2014 at 22:11
  • I want to separate search terms from a string and save them to an array. In my example the top code block would be the string input and the bottom code block would be the array output. Commented Nov 1, 2014 at 5:07

2 Answers 2

5

The code below will parse your query string and split it up into positive and negative search terms.

// parse the query string
$query = 'this -that "-that" "the other thing" -"but not this" ';
preg_match_all('/-*"[^"]+"|\S+/', $query, $matches);

// sort the terms
$terms = array(
    'positive' => array(),    
    'negative' => array(),
);
foreach ($matches[0] as $match) {
    if ('-' == $match[0]) {
        $terms['negative'][] = trim(ltrim($match, '-'), '"');
    } else {
        $terms['positive'][] = trim($match, '"');
    }
}

print_r($terms);

Output

Array
(
    [positive] => Array
        (
            [0] => this
            [1] => -that
            [2] => the other thing
        )

    [negative] => Array
        (
            [0] => that
            [1] => but not this
        )
)
Sign up to request clarification or add additional context in comments.

2 Comments

it fails on "-that" which is not the same as -that. It should be positive (hyphen)that
@UziTech Sorry, that term was not in your OP so I did not take that one into account. I have updated the code now to support it.
0

For those looking for the same thing I have created a gist for PHP and JavaScript

https://gist.github.com/UziTech/8877a79ebffe8b3de9a2

function getSearchTerms($search) {
    $matches = null;
    preg_match_all("/-?\"[^\"]+\"|-?'[^']+'|\S+/", $search, $matches);

    // sort the terms
    $terms = [
        "positive" => [],
        "negative" => []
    ];
    foreach ($matches[0] as $i => $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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.