PHP - Wörter mit der Stoppwortliste filtern

Diese Liste enthält Wörter die aus einem Satz heraus gefiltert werden können. Es handelt sich dabei um keine vollständige Liste, Eigennamen müssen extra hinzugefügt werden.


<?php

// Wörter mit der Stoppwortliste filtern

$stoppwortliste = array("aber","alle","allen","alles","als","also","andere","anderem","anderer",
"anderes","anders","auch","auf","aus","ausser","ausserdem","bei","beide","beiden",
"beides","beim","bereits","bestehen","besteht","bevor","bin","bis","bloss","bloß","brauchen",
"braucht","dabei","dadurch","dagegen","daher","damit","danach","dann","darf",
"darüber","darum","darunter","das","dass","daß","davon","dazu","dem","den","denn","der",
"des","deshalb","dessen","die","dies","diese","diesem","diesen","dieser","dieses","doch",
"dort","dürfen","durch","durfte","durften","ebenfalls","ebenso","ein","eine","einem",
"einen","einer","eines","einige","einiges","einig","entweder","erst","erste","ersten","etwa",
"etwas","falls","fast","ferner","folgender","folglich","für","ganz","geben","gegen","gehabt",
"gekonnt","gemäss","gemäß","getan","gewesen","gewollt","geworden","gibt","habe","haben",
"hätte","hätten","hallo","hat","hatte","hatten","heraus","herein","hier","hin","hinein","hinter",
"ich","ihm","ihn","ihnen","ihr","ihre","ihrem","ihren","ihres","immer","indem","infolge","innen",
"innerhalb","ins","inzwischen","irgend","irgendwas","irgendwen","irgendwer","irgendwie","irgendwo",
"ist","jede","jedem","jeden","jeder","jedes","jedoch","jene","jenem","jenen","jener","jenes",
"kann","kein","keine","keinem","keinen","keiner","keines","können","könnte","könnten","konnte",
"konnten","künftig","leer","machen","macht","machte","machten","man","mehr",
"mein","meine","meinen","meinem","meiner","meist","meiste","meisten","mich","mit","möchte",
"möchten","müssen","müssten","muss","musste","mussten","nach",
"nachdem","nacher","nämlich","neben","nein","nicht","nichts","noch","nützt","nur",
"nutzt","obgleich","obwohl","oder","ohne","per","pro","rund","schon","sehr","seid","sein",
"seine","seinem","seiner","seit","seitdem","seither","selber","sich","sie","siehe","sind","sobald",
"solange","solch","solche","solchem","solchen","solcher","solches","soll","sollen","sollte","sollten",
"somit","sondern","soweit","sowie","später","stets","such","über","ums","und","uns",
"unser","unsere","unserem","unseren","viel","viele","vollständig","vom","von","vor","vorbei",
"vorher","vorüber","während","wäre","wären","wann","war","waren","warum",
"was","wegen","weil","weiter","weitere","weiterem","weiteren","weiterer","weiteres","wem",
"wen","wenigstens","wenn","wenngleich","wer","werde","werden","weshalb","wessen","wie",
"wieder","will","wir","wird","wodurch","wohin","wollen","wollte","wollten","worin",
"würde","würden","wurde","wurden","zufolge","zum","zusammen","zur","zwar",
"zwischen");

$string "eine erfahrung kann man machen, nicht kaufen.";
$string str_replace(array("."","";"":""-"), ""$string);

foreach (
$stoppwortliste as $element) {
 
$string str_replace(" " $element " "" ",  " " $string " ");
}
echo 
$string// erfahrung kaufen
?>

Um die Wörter in einer Suchanfrage zu filtern, habe ich bei einem Projekt mal dieses Script verwendet:


<?php

// Anzahl der Wörter ermitteln und mit der Stoppwortliste filtern

if (count(preg_split('/[\s]+/'$_POST["suchanfrage"])) > 0) {
 
$words explode(' '$_POST["suchanfrage"]);
 foreach (
$words as $nr => $word) {
  if (
in_array($word$stoppwortliste)) {
   unset(
$words[$nr]);
  }
 }
 
$_POST["suchanfrage"] = implode(' '$words);
}
?>

Bausteine  Alle Anzeigen

Eine zufällige Auswahl von Codeschnipseln aus den Bereichen HTML, CSS, PHP, JavaScript und MySQL.

<progress> Fortschrittsanzeige

CSS - Diashow mit Keyframes

PHP - Alle Formulardaten ausgeben

JavaScript - Browser Infos

MySQL - Spalten-Durchschnitt ermitteln

Fehlerhafter Quelltext
Die meisten Browser sind wahre Lesekünstler. Sie versuchen selbst aus dem größten Code-Chaos ein lesbares Bild für den Betrachter zu schaffen. Je mehr Fehler allerdings im Quelltext vorhanden ist, desto mehr Zeit wird er für die Darstellung benötigen. Eine fehlerfreie Ausgabe ist gleichermaßen zu bezweifeln.