A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearAllowing Only Certain Characters In PHP - page 1
User InfoPosts
Allowing Only Certain Characters In PHP#1
I need to check to see if a variable contains anything OTHER than a-z A-Z 0-9 and the "." character (full stop). Any help would be appreciated.

posted date: 2008-12-28 05:48:00


Re: Allowing Only Certain Characters In PHP#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-28 05:48:01


Re: Allowing Only Certain Characters In PHP#3
if (preg_match("/[^A-Za-z0-9.]/", $myVar)) { // make something}The key point here is to use "^" in the [] group - it matches every character except the ones inside brackets.

posted date: 2008-12-28 05:53:00


Re: Allowing Only Certain Characters In PHP#4
if (preg_match('/[^A-Z\d.]/i', $var)) print $var;

posted date: 2008-12-28 06:09:00


Re: Allowing Only Certain Characters In PHP#5
I think that this would only detect strings consisting of only the unspecified characters - it might be better to use /[^A-Za-z0-9\.]/

posted date: 2008-12-28 06:44:00


Re: Allowing Only Certain Characters In PHP#6
Much better. But I still suggest /[^A-Z\d.]/i since it's clearer and less to type. And you don't need to escape the "." inside character classes.

posted date: 2008-12-28 06:50:00


Re: Allowing Only Certain Characters In PHP#7
There are two ways of doing it.Tell whether the variable contains any one character not in the allowed ranges. This is achieved by using a negative character class [^...]:preg_match('/[^a-zA-Z0-9\.]/', $your_variable);Th other alternative is to make sure that every character in the string is in the allowed range:!preg_match('/^[a-zA-Z0-9\.]*$/', $your_variable);

posted date: 2008-12-28 06:52:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.