php remove stop words from string

Solutions on MaxInterview for php remove stop words from string by the best coders in the world

showing results for - "php remove stop words from string"
Jorge
27 Jan 2017
1  public function optimizeSearchString($searchString = "")
2  {
3    $stopwords = array(
4      'der' => 1,
5      'die' => 1,
6      'das' => 1,
7      'the' => 1);
8
9    $words = preg_split('/[^-\w\']+/', $searchString, -1, PREG_SPLIT_NO_EMPTY);
10
11    if (count($words) > 1) {
12      $words = array_filter($words, function ($v) use (&$stopwords) {
13        return !isset($stopwords[strtolower($v)]);
14      }
15      );
16    }
17
18    if (empty($words)) {
19      return $searchString;
20    }
21
22    return implode(" ", $words);
23  }