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,870 points
564 views

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,770 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,300 points
13,362 views
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
1,405 views
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,840 points
23,338 views
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
7,749 views
+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,790 points
4,077 views
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
779 views
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,770 points
513 views
+1 vote
4 answers

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

Delete the List and its element: We have ...READ MORE

answered Jun 7, 2020 in Python by sahil
• 500 points
280,738 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP