Jump to content

[SOLVED] Trying to set up a restricted word list


Muffin

Recommended Posts

I'm trying to set up a registration page that, before creating the account, queries a table to find if the username the person selected contains a word in said table.  I tried using a SELECT LIKE until I realized (about 3 hours later) that it won't work unless the username is exactly one of the restricted words, or shorter.

 

If that's not too clear, here's an example;

 

User selects the name "{DvT}Muffin"

If the word {DvT} exists in the restricted names list, then stop the registration and return an error.

 

I'm pretty new to php, but even if a solution is rather complex, throw it at me anyway.

Basically, rather than using your username to query against the table, I would select your list of restricted words and match your username to them instead. For instance:

<?php
$username = "DvTMuffin";

$valid = TRUE; // default to true unless you find a match
$sql = mysql_query("SELECT word FROM restricted_table");
while ($x = mysql_fetch_array($sql)) {
  if (preg_match("|" . $x['word'] . "|i", $username)) {
    $valid = FALSE;
  }
}

if ($valid) {
  // Process username
}
?>

I'll give that a try in a sec, but a question.  Wouldn't that only check if the restricted word was at the start of the name.  I would like it to check for the restricted word wherever it may be in the name.

 

No, as it is, it will simply check to see if the word appears anywhere in the name. If you want to check that it starts with the word, you'd need to modify your regexp to something like this:

<?php
$regexp = "|^$x[word]|i";
if (preg_match($regexp, $username)) {
  $valid = FALSE;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.