Jump to content

Recommended Posts

I have this textarea, well, registration to be exact. I was wondering if there is a way to filter certain words from getting into the database when signing up. Things such as spaces and inappropriate/bad words. I only have 3 fields at the moment, which are

 

$username

$password

$email

 

Is there any way to filter those? Thanks in advance.

str_replace will replace all words associated within $badwords with nothing. If you wish to replace them with certain text, use the second parameter of str_replace and add whatever you want displayed. If you wish to filter words simply edit/add to what barand has shown

Edited by ExtremeGaming

str_replace will replace all words associated within $badwords with nothing. If you wish to replace them with certain text, use the second parameter of str_replace and add whatever you want displayed. If you wish to filter words simply edit/add to what barand has shown

 

Well thank you very much! One last thing, does this count for spaces? I want to remove that from usernames as well.

<?php
$username = "abc microsoft uses aspx with .net";

$badwords = array('microsoft', 'asp', '.net', ' ');

$clean = str_replace ($badwords, '', $username);

echo $clean;    // --> abcusesxwith
?>

I would never silently edit user input like this, especially not for things such as usernames, e-mail addresses and such! (Profanity filters is something else.)

 

What you should do, is to use the ctype_* () functions to validate the input according to certain rules. These rules should clearly define what input you expect to be given by the user, and show a warning if anything falls outside of these rules. This is commonly known as "input validation", and there's whole lot of information about this on the net. Some good, but unfortunately a lot of bad stuff too.

You should also learn about "output escaping", which is a completely separate concern from input validation, but they're almost always both employed in the same code. Unfortunately that has lead some people to mix them, and treat them as the same. This include people a lot of who have written "security tutorials", which means you might have to search a bit to find some good ones.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.