String eines FormularFeldes in einzelne Worte zerlegen


<html><head><title>Anzeige</title></head> <body bgcolor="#eeeeee"><pre> <h2>String eines FormularFeldes in einzelne Worte zerlegen</h2><hr noshade size="1"> <b> Anm.:</b> W&ouml;rter in Hochkommatas werden als ein Wort erkannt.<hr noshade size="1"> <table border=0><tr> <form action="<? echo $PHP_SELF ?>" method="post"> <td><input type="text" name="formInput" len="30" maxlen="30" value='<? echo isset($formInput) ? stripslashes($formInput) : "Such PHP Array" ?>'></td> <td><input type="submit" value="Zerlegen"></td> </form> </tr></table><hr noshade size="1"> <? // === Funktion: str_words ================= // // Zerlegt String in einzelne Woerter (HK-Ausertung) function str_words($formInput) { $HK = "&quot;"; // HK nach htmlspecialchars // a. Worte nach BLANKS aufsplitten $arr = split(" ",trim($formInput)); // b. HK-Worte zusammenfuegen (HK = HochKomma) for($str_HK="",$i=0;$i<count($arr);$i++) if ( strlen($arr[$i]) > 1 ) // Word-Länge > 1 if ( strlen($str_HK) > 0) { // * HK-Modus $str_HK .= " ".$arr[$i]; // -> HK-Woerter zus.fuegen if ( ereg($HK,$arr[$i]) ) { // mit HK: Ende $arrRet[] = $str_HK; // -> HK-Wort ins Array $str_HK = ""; } } else // * nicht HK-Modus if ( ereg($HK,$arr[$i]) ) // hat HK: Start $str_HK = $arr[$i]; else $arrRet[] = $arr[$i]; // c. Worte um HKs u. BLANKs bereinigen for($i=0; $i<count($arrRet); $i++) { $arrRet[$i] = str_replace($HK," ",$arrRet[$i]); // HK in Blanks $arrRet[$i] = trim($arrRet[$i]); // Blanks weg } return $arrRet; } // === ProgrammAblauf ================== // if ( isset($formInput) ) { $formInput = stripslashes($formInput); // Slashes entfernen $formInput = htmlspecialchars($formInput); // Sonderzeichen wandeln echo "\n<b>Eingabe:</b>_".$formInput."_<p>"; echo "\n<b>W&ouml;rter:</b><br>"; $arr = str_words($formInput); // Suchworte ermitteln for($i=0; $i<count($arr); $i++) // Suchworte ausgeben echo " ".($i+1).". ".$arr[$i]."<br>"; } ?> </pre></body></html>