Skip to content
PHP Logo

How can "häufige Wörter" be effectively filtered out in a PHP search function?

When filtering out "häufige Wörter" (common words) in a PHP search function, you can create an array of common words and check if the search query contains any of those words. If a common word is found, you can exclude it from the search results. This can be done by comparing the search query with the array of common words and removing any matches before performing the search.

php
// Array of common words to filter out
$commonWords = array("the", "and", "is", "in", "it", "on");

// Get the search query
$searchQuery = $_GET['query'];

// Check if search query contains any common words
foreach ($commonWords as $commonWord) {
    if (stripos($searchQuery, $commonWord) !== false) {
        $searchQuery = str_ireplace($commonWord, "", $searchQuery);
    }
}

// Perform the search with the filtered query
// Your search function here