The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > Web Programming > PHP


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 06-28-2007, 10:00 AM
siLenTz's Avatar
siLenTz siLenTz is offline
Jr. Programmer
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Posts: 87
iTrader: (0)
siLenTz will become famous soon enoughsiLenTz will become famous soon enough
Regular Expressions In PHP (Part 3)

You have read the first and second part already right? Now it get your
hand dirty with more regular expression in PHP lesson. In this lesson,
I will introdce you 4 more special characters that are used in regular
expressions.

The first I want to introduce is ?. ? mean optional. For example, "me?n"
which mean letter e is optional. The string that match this expression,
it should be "men" or "mn" but not "man".

() is also regular expression special character. Its job is to group characters
into a string. For simple example: "^(ab)" match only with string that is
start with letter ab. Another example: "n(am)e" will only match with
"name" but not "nae" or "ne". Another useful example, "^(ab)+" match with
string that contains one or more "ab" at the beginning. Now let use () and ?
together:

PHP Code:
    $regexp "^[A-Za-z]+([0-9]+)?$";
    
$string "hello22";

    if (
ereg($regexp$string)) {
        print 
"Match...!";
    } else {
        print 
"Not Match...!";
    } 
Let analyze what this regular expression, "^[A-Za-z]+([0-9]+)?$", does.
"^[A-Za-z]+" mean only string that start with alphabet character, and
"([0-9]+)?$" mean it is optional to have number at the end of the string.
So this expression will match with "hello", "hello2007" and "man18" but
not match with "L1fE" or "se7en".

Full-stop(.) mean any single character. For example, ".." match with
any string that contain at least 2 characters. Another example,
"^..$" match with any string that contain exactly 2 characters.
"............" there are 10 full-stops in this quote which mean only string
that contains at least 10 characters. But there are shorter way to do
it by using {}. {} mean repeating. ".{10}" is equal to ".........." and
"a{5}" is equal to "aaaaa". But {} give a powerful function that
allow us to determine the range of repeatation. For example:
"a{2,5}" mean it match to string that have at least 2 of letter a but must
be less than 6 of a letter.

I will write a very powerful email validation by using a very complex
regular expression:

PHP Code:
<?php
    $regexp 
"^[A-Za-z0-9._-]+@[A-Za-z0-9_-]+(\.[A-Za-z]+){1,2}$";
    
$string "invisal@gmail.com";

    if (
ereg($regexp$string)) {
        print 
"Match...!";
    } else {
        print 
"Not Match...!";
    }
?>

"^[A-Za-z0-9._-]+@[A-Za-z0-9_-]+(\.[A-Za-z]+){1,2}$"
is match with
"invisal@gmail.com", "invisal@programmestalk.net" and "admin@admin.co.nr"
but not match with "invisal@he.co.kh.gov" because it is not possible
to have "he.co.kh.gov" domain name. Complicated? Let me explain,
"^[A-Za-z0-9._-]+@" allow only alphabet character, numbers and
".", "_" or "-" at beginning of string until it reach character @.
"[A-Za-z0-9_-]+" for domain name only allow alphabet character, numbers,
"_" and "-" until it reach it character "." in here I used "\." instead of "."
because if I use only ".", the regular expression perform "." as allow any
single character which is function of "." do in regular expression. But
I use \. mean I only need a simple fullstop character. "(\.[A-Za-z]+){1,2}$"
match any string that end with "." + alphabets and it could only be repeat
2 times. It match ".com", ".com.kh", ".us" and "co.nr" but not match with
".com.net.org" or "co.uk.com"....

That's all for the Basic Regular Expression, I hope you enjoy my tutorial
from the first part until the last part. If you don't understand any part
you can ask me questions. I glad to help I. You can tell me, If you need
more useful regular expression, I will post it on this website

---------------- END --------------------------

From siLenTz
Regular expression journey is ending here....

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by siLenTz : 06-28-2007 at 10:05 AM.
Reply With Quote
The Following 3 Users Say Thank You to siLenTz For This Useful Post:
HelloWorld (06-28-2007), Lee (06-28-2007), TeraTask (06-29-2007)
  #2 (permalink)  
Old 06-28-2007, 12:48 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 616
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Thanks a lot, all 3 tutorials have helped me build up a little on this function, now all i need to do is put it together which i will be doing in a script im making

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to Lee For This Useful Post:
TeraTask (06-28-2007)
  #3 (permalink)  
Old 06-28-2007, 01:35 PM
siLenTz's Avatar
siLenTz siLenTz is offline
Jr. Programmer
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Posts: 87
iTrader: (0)
siLenTz will become famous soon enoughsiLenTz will become famous soon enough
Quote:
Originally Posted by Lee View Post
Thanks a lot, all 3 tutorials have helped me build up a little
on this function, now all i need to do is put it together which i will be doing in
a script im making
Your thank give me a great motivation to write my next tutorial (which I
don't know what is going to be about) Thanks you...

From siLenTz
Thanks....

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to siLenTz For This Useful Post:
TeraTask (06-28-2007)
  #4 (permalink)  
Old 06-28-2007, 02:18 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Thank you even though I'm not able to use your tutorial yet, since I'm now overwhelmed with various programming problems

I will surely try out your tutorial once I get done with this problem
P.S each time there's tutorial written, it just make me excited and wanting to try them out all, but I can't do multithreading (for human) lol... we don't have dual core... sigh... multithreading is a headache for me now...

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #5 (permalink)  
Old 06-29-2007, 12:45 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 616
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Ok im using this in my script now that im working on, at the moment my string is:
PHP Code:
    $regexp "^[A-Za-z0-9]"
That means that they can only use letters and numbers to start, i want to make it so thats all they can do, at the moment i could register "admin-#" i have tried:
PHP Code:
    $regexp "^[A-Za-z0-9]$"
But then it will not let any username be correct? Am i doing something wrong or is there a different string to do this?

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #6 (permalink)  
Old 06-29-2007, 01:14 PM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Staff*
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 428
iTrader: (0)
TeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enough
Lee, I don't understand your problem with the first regular expression. You want it to start with a letter. admin-# starts with "a" which is a letter.

Your second expression only allows a 1 character string (start and end are set and you've give one character in-between).

If you're trying to sanitize a username, let me recommend a different approach. I don't know the ereg syntax, so I'll give it to you with perl expressions.

PHP Code:
<?php
$username 
"admin-#";

//Strip out all non-letters and numbers from the entire string.
$username_sanitized preg_replace('/[^a-z0-9]/i','',$username);

if (
$username_sanitized == $username) {
  
//No bad characters were removed, so this is a good username
  
echo "<p>Good username.</p>";
} else {
  
//At least one character was removed
  
echo "<p>Bad username.  Try ".$username_sanitized." instead.</p>";
}
?>

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #7 (permalink)  
Old 06-29-2007, 01:16 PM
siLenTz's Avatar
siLenTz siLenTz is offline
Jr. Programmer
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Posts: 87
iTrader: (0)
siLenTz will become famous soon enoughsiLenTz will become famous soon enough
PHP Code:
$regexp "^[A-Za-z0-9]"
This regular expression mean only alphabet or number at the beginner of
the string would match.

PHP Code:
$regexp "^[A-Za-z0-9]$"
This regular expression mean only one character is allow and that character
must be alphabet or number.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #8 (permalink)  
Old 06-29-2007, 01:26 PM
siLenTz's Avatar
siLenTz siLenTz is offline
Jr. Programmer
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Posts: 87
iTrader: (0)
siLenTz will become famous soon enoughsiLenTz will become famous soon enough
I think I get what you are trying to do. what you want is only alphabet or
number allowed in username. Here is the regular expression for it.

PHP Code:
$regexp "^[A-Za-z0-9]+$" 
As I mention in the first part. "+" mean one or more. If you omit the "+", the
regular expression mean only one character allowed in string. But if you
put "+", it mean one or more alphabet or number characters allowed.

From siLenTz
Hope, it is what you want...

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to siLenTz For This Useful Post:
Lee (06-29-2007)
  #9 (permalink)  
Old 06-29-2007, 01:41 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 616
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Thats exactly what i needed, its my first time using php to make a script so i guess im still learning a lot Thanks.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #10 (permalink)  
Old 06-29-2007, 10:05 PM
siLenTz's Avatar
siLenTz siLenTz is offline
Jr. Programmer
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Posts: 87
iTrader: (0)
siLenTz will become famous soon enoughsiLenTz will become famous soon enough
Moreover, I prefer this regular expression "^[A-Za-z0-9]{4,30}$" than
"^[A-Za-z0-9]+$". "^[A-Za-z0-9]{4,30}$" mean only string, that
contain only alphabet or number, is matched, and it must be longer than
3 characters and shorter than 31 characters.

From siLenTz
Hope that is useful...

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
Reply


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

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



All times are GMT -7. The time now is 08:09 AM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50