Current location: Hot Scripts Forums » Programming Languages » PHP » Regular Expressions


Regular Expressions

Reply
  #1 (permalink)  
Old 08-21-04, 04:11 PM
Zapatista Zapatista is offline
Newbie Coder
 
Join Date: Aug 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Regular Expressions

I'm creating a log in/registration on a site I'm making and I want to limit what the people use as their usernames and I know you have to you ereg() but I'm not sure how and I've looked at 2 books and the PHP's official website and I've still got nothing. I want it to allow all letters, numbers, and dashes but nothing else. Please help, I'm stuck.
Reply With Quote
  #2 (permalink)  
Old 08-21-04, 04:34 PM
kvnband kvnband is offline
Wannabe Coder
 
Join Date: Jun 2003
Posts: 242
Thanks: 0
Thanked 0 Times in 0 Posts
I'm still learning regular expressions, but I know that

Code:
^[a-zA-Z0-9]$
is for a-z, A-Z, 0-9 but I'm not sure how you add a hyphen in there.
Reply With Quote
  #3 (permalink)  
Old 08-21-04, 05:15 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
To allow upper & lowercase letters, numbers, spaces, hyphens, underscores, and periods, I use:
PHP Code:

ereg_replace("[^A-z0-9 _-.]"""$username); 

Any characters other than those will be stripped.
Reply With Quote
  #4 (permalink)  
Old 08-21-04, 05:17 PM
kvnband kvnband is offline
Wannabe Coder
 
Join Date: Jun 2003
Posts: 242
Thanks: 0
Thanked 0 Times in 0 Posts
Cool...I didn't know if you could add things like hyphens, etc... right in there or not. (I was wondering because of A-z I thought maybe php would get confused if I added a hyphen)

Kevin
Reply With Quote
  #5 (permalink)  
Old 08-21-04, 05:31 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Yeah, took me a bit to discover that... just start adding them at the end.
Reply With Quote
  #6 (permalink)  
Old 08-21-04, 06:33 PM
Zapatista Zapatista is offline
Newbie Coder
 
Join Date: Aug 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
thanks for all of your help
Reply With Quote
  #7 (permalink)  
Old 08-21-04, 06:42 PM
Zapatista Zapatista is offline
Newbie Coder
 
Join Date: Aug 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
I tried what you suggested and it still won't work. It only works if I just put in a single character. Anymore than that will result in it not matching. Any suggestions?
Reply With Quote
  #8 (permalink)  
Old 08-22-04, 08:54 AM
mikaelf mikaelf is offline
Wannabe Coder
 
Join Date: Jun 2004
Location: php[dot]net
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by kvnband
I'm still learning regular expressions, but I know that

Code:
^[a-zA-Z0-9]$
is for a-z, A-Z, 0-9 but I'm not sure how you add a hyphen in there.
What's wrong with above code:
^[a-zA-Z0-9]$ -> will only evalute true for single character not many.

Quote:
Originally Posted by keith
To allow upper & lowercase letters, numbers, spaces, hyphens, underscores, and periods, I use:
PHP Code:

ereg_replace("[^A-z0-9 _-.]"""$username); 


Any characters other than those will be stripped
What's wrong with above code:
1. A-z should be A-Z or a-z
2. The operator ^ in square bracket [] will behave as negation and not assertion. Thus ^A-Z means you disallow alphabet A through Z
3. The dot (.) refers to all characters except newline, if you want to include the dot itself, cast it first using \
4. As with kvnband, the regex will only evaluate single character.

Valid regular expression should be:
$isMatch = preg_match('/^[a-zA-Z_-\.]+$/',$username);
or
$isMatch = preg_match('/^[a-z_-\.]+$/i',$username);
or
$isMatch = preg_match('/^[a-z_\-\.]+$/i',$username); //safely cast dash

Notes:
1.It's much better to use preg_match than ereg because it will evaluate faster (you may give a try for speed test)
2.Before using the regex test function, trim the trailing spaces by using php function trim()
__________________
Useful PHP links:
bugs.php.net - for reporting PHP bugs
pear.php.net - PHP extension and application repository
pecl.php.net - get non standard PHP modules, submit yours
www.phpclasses.org - PHP classes repository
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
is it a good idea to run 23 search expressions? lordmerlin ASP 1 03-24-04 04:05 PM
Regular Expressions Trevor PHP 7 01-19-04 06:15 PM
suggest a regular expression gmadhukarreddy Perl 2 01-08-04 12:56 AM
Regular Expressions helpcenterlive PHP 2 12-08-03 06:56 PM
Regular Expressioning []'s AbelaJohnB PHP 8 12-04-03 02:37 PM


All times are GMT -5. The time now is 04:23 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.