How to check the entry of forbidden words in a MySQL database?

Hypothetically, if I did not want the word "douche" to be anywhere in the username, and I have a table in my database with all the forbidden 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 "invalid";
}
else
{
...etc

The problem is that I don’t know how to execute a query that will receive partial matches (e.g. Jdoucher or douchebag4). I know that %% username% is obviously wrong. Does anyone know how to do this? Is it possible? Thank.

+3
source share
4 answers
select *
    from restrictions 
    where locate(prohibited, @username) <> 0
+3
source

You would do the opposite of the example you gave. First get all the banned keywords, then find each word in the username:

if (stripos($username, $word) !== FALSE) {
   // uhoh
}
+1

, simshaun, , .

Of course, if your word database is too long, you look at processing time and high memory requirements.

0
source

You can accomplish this with regex .

0
source

Source: https://habr.com/ru/post/1780932/


All Articles