Checking for non-letter characters?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Checking for non-letter characters?

Post by Citizen »

Cheers,

I searched php.net and phpdn forums for an answer for this but couldnt find it.

I want to check to make sure that users dont have any characters that arent letters or numbers like symbols and stuff.

How can this be accomplished?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: Checking for non-letter characters?

Post by timvw »

Citizen wrote:Cheers,

I searched php.net and phpdn forums for an answer for this but couldnt find it.

I want to check to make sure that users dont have any characters that arent letters or numbers like symbols and stuff.

How can this be accomplished?
Sometimes a problem becomes easier if it you think about the inverse.

(1) The inverse to your problem is verifying if a string only contains letters or digits

(2) A possible solution for (1) is the following is to check if the string preg_matches #^\w+$#

(3) Back to your original problem, if there is a string that does not preg_match the expression in (2) there is invalid data ;)

(4) Time for improvement: \W matches everything that is not a a letter or digit. So if any of the input strings matches \W there is a problem.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

viewtopic.php?t=45203 may be of interest to both of you.
Post Reply