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
    – Travesty3
    Commented May 9, 2012 at 14:44
  • What about when somebody's name is "Youngs"?
    – Sampson
    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 (===, !==)
    – Cole Tobin
    Commented May 9, 2012 at 14:45
  • This will then be turned into a bad word filter.
    – Frank
    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?
    – Sampson
    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;
}
2
  • Why? There is a function to that: str_word_count Commented Nov 18, 2019 at 14:04
  • @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) Commented Nov 19, 2019 at 9:22
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";
0
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
  • function stringTester() not found? Commented Mar 7, 2013 at 13:54
  • 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; } }
    – ffdigital
    Commented Mar 31, 2016 at 15:50
  • 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 )); }
    – ffdigital
    Commented Mar 31, 2016 at 15:50
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" ;
}
0
<?php
function isUserNameCorrect()
{
    Foreach($badname in $badnames)
    {
        if(strstr($badname, $enteredname) == false)
        {
             return false;
        }
    }
    return true;
}

Maybe this helps

1
  • 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. Commented May 9, 2012 at 14:53
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";
}
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.

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
  • shouldn't it be if (strpos($bad_word,$sentence) !== false) { $match = true; break; }
    – Liam
    Commented Aug 29, 2015 at 13:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.