5

I am storing a list of prohibited words in an array:

$bad = array("test");

I am using the below code to check a username against it:

if (in_array ($username, $bad))
{
//deny
}

but I have a problem in that it only denies if the given username is exactly test, but I want it to also deny if the given username is Test, or TEST, or thisisatestok, or ThisIsATestOk.

Is it possible?

1
  • Use strpos for example or read about regular expressions and preg_* functions Commented Jan 7, 2015 at 9:26

5 Answers 5

5

Although the other answers use regex and the preg_* family, you're probably better off using stripos(), as it is bad practice to use preg_* functions just for finding whether something is in a string - stripos is faster.

However, stripos does not take an array of needles, so I wrote a function to do this:

function stripos_array($haystack, $needles){
    foreach($needles as $needle) {
        if(($res = stripos($haystack, $needle)) !== false) {
            return $res;
        }
    }
    return false;
}

This function returns the offset if a match is found, or false otherwise.
Example cases:

$foo = 'evil string';
$bar = 'good words';
$baz = 'caseBADcase';
$badwords = array('bad','evil');
var_dump(stripos_array($foo,$badwords));
var_dump(stripos_array($bar,$badwords));
var_dump(stripos_array($baz,$badwords));
# int(0)
# bool(false)
# int(4)

Example use:

if(stripos_array($word, $evilwords) === false) {
    echo "$word is fine.";
}
else {
    echo "Bad word alert: $word";
}
Sign up to request clarification or add additional context in comments.

Comments

4

By filtering each word in array with case insensitive regex , you can get the list of words contains needle.

<?php
$haystack = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
$needle = 'DAY';
$matches = array_filter($haystack, function($var){return (bool)preg_match("/$needle/i",$var);});
print_r($matches);

outputs :

Array
(
    [0] => sunday
    [1] => monday
    [2] => tuesday
    [3] => wednesday
    [4] => thursday
    [5] => friday
    [6] => saturday
)

3 Comments

Please elaborate on your answer.
While this might answer the question, it's custom to at least add some comments or explanation to your answer so people might actually learn something instead of copy / pasting code.
thanks for the advices , just elaborated the answer :)
1
$example = array('An example','Another example','One Example','Last example');
$searchword = 'last';
$matches = array();
foreach($example as $k=>$v) {
    if(preg_match("/\b$searchword\b/i", $v)) {
        $matches[$k] = $v;
    }
}

Works with substrings and case insensitive.

Found here: Search for PHP array element containing string

Comments

0

You can do with strtolower()

$words = array('i', 'am', 'very', 'bad');

$username = "VeRy";
$un = strtolower($username);

if (in_array($un, $words)){
    //found !
}

Comments

0

Not the answer to the question, but to the title (PHP in_array wildcard match):

function in_array_wildcards(string $needle, array $haystack, bool $case_insensitive = true):bool {
    foreach ($haystack as $value) {
        if (strpos($value, '*') !== false) {
            if ((bool)preg_match('/' . str_replace('*', '(.*)', $value) . '/' . (($case_insensitive) ? 'i' : ''), $needle)) {
                return true;
            }
        }
        else {
            if ($case_insensitive) {
                if (strtolower($value) == strtolower($needle)) {
                    return true;
                }
            }
            else {
                if ($value == $needle) {
                    return true;
                }
            }
        }
    }
    return false;
}

Works perfectly for

if (!in_array_wildcards($sFile, array('.htaccess', 'config.php', 'dbfs.php', 'error.php', 'front_content.php*', 'front_crcloginform.inc.php', 'index.php', 'robots.txt'))) {

('front_content.php*' finds front_content.php, front_content.php.sic, front_content.php.s01, ...)

Comments

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.