How do I check an input against prohibited words in MySQL database?
Hypothetically, if I didn't want to allow the word "douche" anywhere in a username and I have a table in my database with all of the prohibited words...
$q = "SELECT * FROM restrictions WHERE prohibited LIKE '%username%'";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
if (mysqli_num_rows($r) !== 0)
{
//username is prohibited
echo "invali开发者_如何学编程d";
}
else
{
...etc
The problem is that I don't know how to execute a query that would pick up partial matches (ie. Jdoucher, or douchebag4). The %username% part is obviously wrong, I know. Does anyone know how to do this? Is it even possible? Thanks.
select *
from restrictions
where locate(prohibited, @username) <> 0
You'd do the opposite of the example you gave. Get all of the prohibited keywords first then look for each word in the username:
if (stripos($username, $word) !== FALSE) {
// uhoh
}
Using a regex match based on simshaun's answer would probably be the faster answer.
Of course if your database of words is too long you're looking at time to process and large memory requirements.
You can accomplish this with regex.
精彩评论