<?php

// words to be removed
$stopwords = array(
'der' => 1,
'die' => 1,
'das' => 1,
'the' => 1);
# use words as key for better performance

// remove stopwords from string
function strip_stopwords($str = "")
{
  global $stopwords;
  
  // 1.) break string into words
  // [^-\w\'] matches characters, that are not [0-9a-zA-Z_-']
  // if input is unicode/utf-8, the u flag is needed: /pattern/u
  $words = preg_split('/[^-\w\']+/', $str, -1, PREG_SPLIT_NO_EMPTY);
  
  // 2.) if we have at least 2 words, remove stopwords
  if(count($words) > 1)
  {
	$words = array_filter($words, function ($w) use (&$stopwords) {
	  return !isset($stopwords[strtolower($w)]);
	  # if utf-8: mb_strtolower($w, "utf-8")
	});
  }
  
  // check if not too much was removed such as "the the" would return empty
  if(!empty($words))
	return implode(" ", $words);
  return $str;
}

// test it
echo strip_stopwords("The Hobbit das foo, der");
