Does array element contain substring

0 votes

I'd like a function that checks whether an array's items contain a string. As such:

array(1 => 'Super-user', 'Root', 'Admin', 'Administrator', 'System', 'Website', 'Owner', 'Manager', 'Founder');

And then checking for admin12 should return true as a part of admin12 (admin) is also a part of the array.

I came this far:

$forbiddennames= array(1 => 'Super-user', 'Root', 'Admin', 'Administrator', 'System', 'Website', 'Owner', 'Manager', 'Founder');    

if(in_array( strtolower($stringtocheck), array_map('strtolower', $forbiddennames))){
        echo '"This is a forbidden username."';
    } else {
        echo 'true';

Only this only echos "This is a forbidden username." when I check for admin. I want it also to echo when checking for admin12.

Is this possible (and how)?

Nov 26, 2020 in Python by anonymous
• 8,880 points

1 answer to this question.

0 votes

Loop through the $forbiddennames array and use stripos to check if the given input string matches any of the items in the array:

function is_forbidden($forbiddennames, $stringtocheck) 
    foreach ($forbiddennames as $name) {
        if (stripos($stringtocheck, $name) !== FALSE) {
            return true;

And use it like below:

if(is_forbidden($forbiddennames, $stringtocheck)) {
    echo "This is a forbidden username.";
} else {
    echo "True";
answered Nov 26, 2020 by Gitika
• 65,930 points

Related Questions In Python

0 votes
1 answer

Python Pandas: selecting element in array column

pa.loc[row] selects the row with label row. pa.loc[row, ...READ MORE

answered May 13, 2019 in Python by SDeb
• 13,290 points
0 votes
1 answer

Reconstruct an array by replacing every element with a[[i-1]%K] - Python

Hey varsha, Have a look at this ...READ MORE

answered Jun 14, 2019 in Python by Miya
0 votes
1 answer

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Hello @kartik, Since different users might have different ...READ MORE

answered Jun 15, 2020 in Python by Niroj
• 82,780 points
0 votes
0 answers

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I couldn't figure out the problem. here my ...READ MORE

Dec 17, 2020 in Python by muammer
• 120 points
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
0 votes
1 answer

Removing surrounding whitespace

Try the strip() function:  s = s.strip() If you ...READ MORE

answered May 4, 2018 in Python by Nietzsche's daemon
• 4,260 points
0 votes
1 answer

Does Python have a string 'contains' substring method?

You can use the in operator: if "blah" not in ...READ MORE

answered Nov 26, 2020 in Python by Gitika
• 65,930 points
0 votes
4 answers

How do I remove an element from a list by index in Python?

1886 Use del and specify the index of the element ...READ MORE

answered Dec 11, 2020 in Python by Gitika
• 65,930 points