Forum Moderators: coopster

Message Too Old, No Replies

How to remove special characters from a string

         

impact

5:25 am on Dec 16, 2009 (gmt 0)

10+ Year Member



Hello,

I have a members account in my site. When new members register they get to create their own user name. A sub folder is created with the same user name.

I would like to prevent new users from using special characters from their users name such as - / ) .! # and so on.

some one please show me a tutorial which explains how to use PHP functions to do this?

Any help will be appreciated.

Thank you,

ogletree

5:56 am on Dec 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$string = ereg_replace("[^A-Za-z0-9]", "", $string );

ChrisE

8:06 am on Dec 16, 2009 (gmt 0)

10+ Year Member



I'd suggest not removing the special characters from the string. Instead, if the values don't fit your rules, reject the attempt and request the user to do it again.

Use ctype_alpha or ctype_alnum

So something like:

$username = $_POST['username'];

if(ctype_alpha($username))
{
//insert your code for success
}
else
{
//insert your code for a failed username
}

TheMadScientist

9:42 am on Dec 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'd also recommend if you do anything with regular expressions you avoid ereg... Not only is it slower than preg because it runs on a different engine, it's deprecated as of PHP 5.3 and removed from PHP 6.

[us3.php.net...]

impact

4:01 pm on Dec 16, 2009 (gmt 0)

10+ Year Member



Thank you all for the help.

ChrisE .. I am not very sure about this one. Can you please tell me what kind of character this will allow and what kind of characters will it block?

Thank you,

ChrisE

5:57 pm on Dec 16, 2009 (gmt 0)

10+ Year Member



ctype_alpha checks to see if every character in the string is alphabetic.
ctyple_alnum checks to see if every character is alphanumeric

These are part of the character type checking functions available. . . read about them here:
[us.php.net...]

rocknbil

8:34 pm on Dec 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Then you have to do an or if you want to allow letters and numbers. preg_replace/match makes it a little more extensible. You can also allow dashes, underscores, if you want . . . To bounce back to page for correct entry,

$username_pattern = 'a-z\d\-\_';
// or, following OP example, 'a-z\d'
// note that the following are IDENTICAL:
// /[A-Za-z0-9]/
// /[A-Z0-9]/i <-- case insensitive
// /[A-Z\d]/i
// /[\d\w]/ <-- May or may not work for characters;
// any digit, any word character, which may or may not
// include invalid characters depending on charset.

if (preg_match('/[^$username_pattern]/i', $string )) {
// re-output the form with warning
// ^ when first in a class [] means "not." So
// anything NOT these characters will fall here.
}
else {
// do the signup
}

A very good piece of advice that may seem small,

reject the attempt and request the user to do it again.

User thinks their user name was accepted, you FIX it, so when they log in, it will reject the log in because they don't know you altered it.

impact

3:06 am on Dec 17, 2009 (gmt 0)

10+ Year Member



Thank you, Thank you, Thank you