1 Replies - 2632 Views - Last Post: 06 November 2010 - 04:17 PM Rate Topic: -----

#1 cab3k3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-November 10

Filtering a username of all spaces and special characters

Posted 06 November 2010 - 04:05 PM

Hello, I've come across a seemingly easy problem, however, I've been unable to fix it. What I want to happen is when a user inputs a username it is only to be text, nothing more. The FILTER_SANITIZE_STRING function seems to be what I want, but when tested my usernames still contain all spaces and special characters. I'm doing this to filter my user input against SQL injections, as well as ensuring my users have bearable usernames. Any help would be greatly appreciated.

$username = $_POST['username'];
$safeUsername = filter_var($username,FILTER_SANITIZE_STRING);

echo $safeUsername;




Is This A Good Question/Topic? 0
  • +

Replies To: Filtering a username of all spaces and special characters

#2 moopet   User is offline

  • binary decision maker
  • member icon

Reputation: 345
  • View blog
  • Posts: 1,190
  • Joined: 02-April 09

Re: Filtering a username of all spaces and special characters

Posted 06 November 2010 - 04:17 PM

Those functions won't do what you need, afaik. If you use a simple regular expression you can customise it to allow whatever characters you want and strip everything else. This one here allows upper- and lower-case letters only:
$safeUsername = preg_replace('/[^a-zA-Z]/', '', $username);


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1