Re: Is there a special character I here? (Regular Expressions)

From: Date: Fri, 17 Aug 2001 00:17:10 +0000
Subject: Re: Is there a special character I here? (Regular Expressions)
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-63113@lists.php.net to get a copy of this message
It's probably a good idea to learn a bit about regular expressions. In short, PHP gives us two types of regular expressions to play with, POSIX and PCRE. For more information : PCRE : http://php.net/manual/en/ref.pcre.php POSIX : http://php.net/manual/en/ref.regex.php The following is an example rich tutorial : http://www.phpbuilder.org/columns/dario19990616.php3 It uses ereg, which is POSIX : ereg() : http://php.net/manual/en/function.ereg.php Your question is "How can I make sure a string contains ONLY alpha-numeric characters?" That could look like : $string = 'examPle123'; if (eregi("^[a-z0-9]+$",$string)) { echo 'String is alpha-numeric'; } else { echo 'String is NOT alpha-numeric'; } Which in english says : if the string : - begins with // ^ - contains one to an unlimited number of // + - ends with // $ the following characters : a-z // all lowercase letters A-Z // comes from i in eregi (case insensitive) 0-9 // all numbers then this is TRUE. Let's say we wanted to make sure the length was between 3-11 characters, tinkering with the regex the following works : if (eregi("^[a-z0-9]{3,11}$",$string)) { echo 'Is alpha-numeric and 3-11 characters in length'; } else { echo 'Is NOT alpha-numeric and/or 3-11 characters in length'; } Allowing spaces in $string is easy as : [a-z0-9 ] Also, some good variable and string functions exist : variables : http://php.net/manual/en/ref.var.php strings : http://php.net/manual/en/ref.strings.php Regards, Philip Olson On Thu, 16 Aug 2001, Brandon Orther wrote: > Hello, > > I am looking for a way to check a string if it has anything other than > Numbers or Letters. Does anyone know how to do this? > Thank you, > > -------------------------------------------- > Brandon Orther > WebIntellects Design/Development Manager > brandon@webintellects.com > 800-994-6364 > www.webintellects.com > -------------------------------------------- > >

« previous php.general (#63113) next »