3

I have looked around the internet for something that will do this but it will only work with one word.

I am trying to build a script that will detect a bad username for my site, the bad username will be detected if the username contains any of the words in an array.

Here's the code I made, but failed to work.

$bad_words = array("yo","hi");
$sentence = "yo";

if (strpos($bad_words,$sentence)==false) {
echo "success";
}

If anybody could help me, I would appreciate it.

7
  • 1
    Possible duplicate: stackoverflow.com/q/1916261/259457 Commented May 9, 2012 at 14:44
  • What about when somebody's name is "Youngs"? Commented May 9, 2012 at 14:44
  • 1
    Don't use == as (0 == false) would return true. If a function returns multiple types, use strict (===, !==) Commented May 9, 2012 at 14:45
  • This will then be turned into a bad word filter. Commented May 9, 2012 at 14:45
  • @frank Can usernames contains spaces or other non-alphanumeric characters? And are you concerned with strings like "yö" which doesn't appear in your filter? Commented May 9, 2012 at 14:46

8 Answers 8

7

use

substr_count

for an array use the following function

function substr_count_array( $haystack, $needle ) {
     $count = 0;
     foreach ($needle as $substring) {
          $count += substr_count( $haystack, $substring);
     }
     return $count;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Why? There is a function to that: str_word_count
@MarceloRodovalho str_word_count has nothing to do with what the op asked. str_word_count returns the number of words in a string. substr_count returns the number of times a word ($needle) is found within a string. And my function substr_count_array does the same thing using an array of $needle(s)
1

You can use this code:

$bad_words = array("yo","hi");
$sentence = "yo you your";
// break your sentence into words first
preg_match_all('/\w+/', $sentence, $m);
echo ( array_diff ( $m[0], $bad_words ) === $m[0] ) ? "no bad words found\n" :
                                                      "bad words found\n";

Comments

1
// returns false or true
function multiStringTester($feed , $arrayTest)
{   
    $validator = false;
    for ($i = 0; $i < count($arrayTest); $i++) 
    {
            if (stringTester($feed, $arrayTest[$i]) === false ) 
            {   
                continue;
            } else 
            {
                $validator = true;              
            }       
    }
    return $validator;
}
//www.ffdigital.net

3 Comments

function stringTester() not found?
function stringTester($feed, $strTest) { $processedFeed = normalize($feed); $processedTest = normalize($strTest); $pos = strpos($processedFeed, $processedTest); if ($pos === false) { //=== false //echo "WORD ".$strTest." NOT FOUND <br />"; return false; } else { //print_r($licitaToFilter); //echo "WORD " . $strTest . " FOUND IN <br />"; //echo " IS ON LINE ".$pos; return true; } }
function normalize ($string){ $trans = array('&agrave;'=>'a', '&aacute;'=>'a', '&acirc;'=>'a', '&atilde;'=>'a', '&auml;'=>'a', '&aelig;'=>'a', '&ccedil;'=>'c', '&egrave;'=>'e', '&eacute;'=>'e', '&ecirc;'=>'e', '&euml;'=>'e', '&igrave;'=>'i', '&iacute;'=>'i', '&iuml;'=>'i', '&ograve;'=>'o', '&oacute;'=>'o', '&ocirc;'=>'o', '&otilde;'=>'o', '&ouml; '=>'o','&ugrave;'=>'u', '&uacute;'=>'u', '&uuml;'=>'u'); $processedString1 = strtolower($string); return (strtr($processedString1, $trans )); }
0

I believe a sentence is more than one single word

Try

$badwords = array("yo","hi");
$string  = "i am yo this is testing";
$word = array_intersect( $badwords,explode(" " , $string));
if(empty($word))
{
    echo "Sucess" ;
}

Comments

0
<?php
function isUserNameCorrect()
{
    Foreach($badname in $badnames)
    {
        if(strstr($badname, $enteredname) == false)
        {
             return false;
        }
    }
    return true;
}

Maybe this helps

1 Comment

solution is not good for the following problem : Bad word 'fool' won't be detected if user type 'ifoolyou!' (you need to think about partial matches also.
0

Unfortunately, you can't give an Array to functions strpos/strstr directly.

According to PHP.net comments, to do this you need to use (or create your own if you want) function like this:

function strstr_array($haystack, $needle) 
{
     if ( !is_array( $haystack ) ) {
         return false;
     }
     foreach ( $haystack as $element ) {
         if ( stristr( $element, $needle ) ) {
             return $element;
         }
     }
}

Then, just change strpos to the new function - strstr_array:

$bad_words = array("yo","hi");
$sentence = "yo";

if (strstr_array($bad_words,$sentence)==false) {
     echo "success";
}

Comments

0

I'd implement this as follows:

$bad_words = array("yo","hi");
$sentence = "yo";

$valid = true;
foreach ($bad_words as $bad_word) {
    if (strpos($sentence, $bad_word) !== false) {
        $valid = false;
        break;
    }
}

if ($valid) {
    echo "success";
}

A sentence is valid unless it contains at least one word from $bad_words. Note the strict checking (i.e. !==), without this the check will fail when the result of strpos is 0 instead of false.

Comments

0

You're close. You can do it the following way:

$bad_words = array("yo","hi");
$sentence = "yo";
$match = false;
foreach($bad_words as $bad_word) {
  if (strpos($bad_word,$sentence) === false) {  # use $bad_word, not $bad_words
    $match = true;
    break;
  }
}
if($match) { echo "success"; }

1 Comment

shouldn't it be if (strpos($bad_word,$sentence) !== false) { $match = true; break; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.