Dies ist für eine Chat-Seite. Ich habe einen $string = "This dude is a mothertrucker"
. Ich habe ein Array von badwords: $bads = array('truck', 'shot', etc)
. Wie kann ich überprüfen, ob $string
eines der Wörter in $bad
enthält?
Bisher habe ich:
foreach ($bads as $bad) {
if (strpos($string,$bad) !== false) {
//say NO!
}
else {
// YES! }
}
Außer wenn ich das tue, wenn ein Benutzer ein Wort in die Liste $bads
eingibt, ist die Ausgabe NEIN! gefolgt von JA! so aus irgendeinem Grund ist der Code, es zweimal durch läuft.
function contains($str, array $arr)
{
foreach($arr as $a) {
if (stripos($str,$a) !== false) return true;
}
return false;
}
1) Der einfachste Weg:
if ( in_array( 'eleven', array("one", "four", "eleven", "six") ))
...
2) Ein anderer Weg (während Arrays zu anderen Arrays hin überprüft werden):
$keywords=array('one','two','three');
$targets=array('eleven','six','two');
foreach ( $targets as $string )
{
foreach ( $keywords as $keyword )
{
if ( strpos( $string, $keyword ) !== FALSE )
{ echo "The Word appeared !!" }
}
}
können Sie das bitte anstelle Ihres Codes versuchen
$string = "This dude is a mothertrucker";
$bads = array('truck', 'shot');
foreach($bads as $bad) {
$place = strpos($string, $bad);
if (!empty($place)) {
echo 'Bad Word';
exit;
} else {
echo "Good";
}
}
Sie können Ihr fehlerhaftes Word-Array umdrehen und die gleiche Prüfung viel schneller durchführen. Definieren Sie jedes fehlerhafte Word als Schlüssel des Arrays. Zum Beispiel,
//define global variable that is available to too part of php script
//you don't want to redefine the array each time you call the function
//as a work around you may write a class if you don't want global variable
$GLOBALS['bad_words']= array('truck' => true, 'shot' => true);
function containsBadWord($str){
//get rid of extra white spaces at the end and beginning of the string
$str= trim($str);
//replace multiple white spaces next to each other with single space.
//So we don't have problem when we use explode on the string(we dont want empty elements in the array)
$str= preg_replace('/\s+/', ' ', $str);
$Word_list= explode(" ", $str);
foreach($Word_list as $Word){
if( isset($GLOBALS['bad_words'][$Word]) ){
return true;
}
}
return false;
}
$string = "This dude is a mothertrucker";
if ( !containsBadWord($string) ){
//doesn't contain bad Word
}
else{
//contains bad Word
}
In diesem Code prüfen wir lediglich, ob ein Index vorhanden ist, anstatt schlechtes Word mit allen Wörtern in der Liste fehlerhafter Wörter zu vergleichen.
isset ist viel schneller als in_array und geringfügig schneller als array_key_exists .
Stellen Sie sicher, dass keiner der Werte in einem fehlerhaften Word-Array auf null gesetzt ist.
isset gibt false zurück, wenn der Arrayindex auf null gesetzt ist.
Setzen und verlassen oder sterben, sobald es irgendwelche schlechten Worte findet, wie diese
foreach ($bads as $bad) {
if (strpos($string,$bad) !== false) {
//say NO!
}
else {
echo YES;
die(); or exit;
}
}
Sie können den Filter auch auf diese Weise durchführen
$string = "This dude is a mothertrucker";
if (preg_match_all('#\b(truck|shot|etc)\b#', $string )) //add all bad words here.
{
echo "There is a bad Word in the string";
}
else {
echo "There is no bad Word in the string";
}
Wollte das?
$string = "This dude is a mothertrucker";
$bads = array('truck', 'shot', 'mothertrucker');
foreach ($bads as $bad) {
if (strstr($string,$bad) !== false) {
echo 'NO<br>';
}
else {
echo 'YES<br>';
}
}
Ich würde diesen Weg gehen, wenn der Chat-String nicht so lang ist.
$badwords = array('motherfucker', 'ass', 'hole');
$chatstr = 'This dude is a motherfucker';
$chatstrArr = explode(' ',$chatstr);
$badwordfound = false;
foreach ($chatstrArr as $k => $v) {
if (in_array($v,$badwords)) {$badwordfound = true; break;}
foreach($badwords as $kb => $vb) {
if (strstr($v, $kb)) $badwordfound = true;
break;
}
}
if ($badwordfound) { echo 'Youre nasty!';}
else echo 'GoodGuy!';
Es gibt ein sehr kurzes PHP-Skript, mit dem Sie schlechte Wörter in einem String identifizieren können, der str_ireplace wie folgt verwendet:
$string = "This dude is a mean mothertrucker";
$badwords = array('truck', 'shot', 'ass');
$banstring = ($string != str_ireplace($badwords,"XX",$string))? true: false;
if ($banstring) {
echo 'Bad words found';
} else {
echo 'No bad words in the string';
}
Die einzelne Zeile:
$banstring = ($string != str_ireplace($badwords,"XX",$string))? true: false;
macht die ganze Arbeit.
$string = "This dude is a good man";
$bad = array('truck','shot','etc');
$flag='0';
foreach($bad as $Word){
if(in_array($Word,$string))
{
$flag=1;
}
}
if($flag==1)
echo "Exist";
else
echo "Not Exist";
Wenn Sie mit array_intersect () arbeiten möchten, verwenden Sie den folgenden Code:
function checkString(array $arr, $str) {
$str = preg_replace( array('/[^ \w]+/', '/\s+/'), ' ', strtolower($str) ); // Remove Special Characters and extra spaces -or- convert to LowerCase
$matchedString = array_intersect( explode(' ', $str), $arr);
if ( count($matchedString) > 0 ) {
return true;
}
return false;
}