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, 12:23 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 1)

This is my first tutorial in this forum. In this tutorial, I will introduce you
to the basic of regular expressions in PHP. Some of you know what is it
and some are don't. Regular expressions is pattern string descibe
the match to other string. It is very powerful too for use to filter the
string that is not in format that you want.

First of all, let me introduce you to ^ in regular expression. ^, this special
character in regular expression mean the beginning of the string.
If the regular expression is ^a so it will match to any string that start with
letter a. Now let apply this regular expression into PHP code.

PHP Code:
<?php
    $regexp 
"^a";
    
$string "apple";

    if (
ereg($regexp$string)) {
        print 
"Match...!";
    } else {
        print 
"Not Match...!";
    }
?>
ereg is php function that is search for string that is matched with regular
expression. In this code my regular expression is "^a", which mean
every string that start with letter a will be matched. So "apple" is string
that start with letter a which is matched. Now if the $string = "boy" then
the string is not matched because it is started with letter b. Pretty simple
right?

Contrasting with ^, $ mean end of the string. For example: "t$" mean
that any string, that has letter t at the end of the string, is matched.

PHP Code:
<?php
    $regexp 
"t$";
    
$string "eat";

    if (
ereg($regexp$string)) {
        print 
"Match...!";
    } else {
        print 
"Not Match...!";
    }
?>
The string "eat" is matched but if $string = "network", it is not matched.
How about this regular expression, "^apple$"? What dose it mean?
It probally mean any string that start with word apple and end with word
apple, so it mean any string that contain exactly only word "apple" is
matched. "applez" or "aapple" is not mathced.

+ mean one or more. For example: "^a+" mean at least there are one or
more a letter at the beginning of string. strings, that are matched this
expression, are "a", "aa", "aaa". Another example: "a+$" mean at least
one or more of letter a are at the end of the string.


NOTE:
What is "a" mean if we don't use any special character? It
mean any string that contain one or more a letter will be match
for example: "apple" Matched, "eat" Matched, "fix" not matched.

This is my first lesson for regular expression, and more interesting thing
will continue in the second part... I hope you enjoy it. Comment
are warmly welcome.

From siLenTz
Regular expressions RULE...

__________________

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 12:26 AM.
Reply With Quote
The Following User Says Thank You to siLenTz For This Useful Post:
HelloWorld (06-28-2007)
  #2 (permalink)  
Old 06-28-2007, 12:45 AM
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: 426
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
Nice. I should point out that
Quote:
Originally Posted by http://us2.php.net/manual/en/function.ereg.php#id6630354
Note: preg_match(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg().

__________________
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
  #3 (permalink)  
Old 06-28-2007, 01: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
Sorry to bother you, But I don't know if the Perl regular expression is
the same as PHP regular expression I mean on the syntax. I heard that
Perl has a very powerful regular expression.

From siLenTz
Thanks you!

__________________

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
  #4 (permalink)  
Old 06-28-2007, 01:03 AM
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: 426
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
Bother? It's no bother. They are not the same. Perhaps you would want to take a look at them and write this article using the Perl Reg Ex ... It usually helps me to learn something to have a goal in mind instead of just mindlessly reading. Just a thought and good point - they're not the same.

__________________
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
  #5 (permalink)  
Old 06-28-2007, 01:28 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
I do little research on PHP expression and Perl expression. There were some
difference but also some are similar. For example: in PHP regex it looks like
this "a+$" but in Perl it looks like this "/a+$/". In Perl, they allow you with more
option like "/a+$/i" mean that it will insensively match the string.. Perl regular
expressions are more complicate than PHP but it is more powerful than PHP
because they have additional function. As I know, Perl regular expressions
also have condition in it. However, my tutorial is about PHP regular expression
and it also cover the basic regular expressions. I believe that if know PHP
regular expressions you can learn Perl regular expression easily because they
have the same concept.

From siLenTz
Same but different....

__________________

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-28-2007, 07:28 AM
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
Intersting tutorial about the regular expression. But sorry if I sound dumb, what is the significant of knowing regular expression in PHP? I mean, how is that useful for our PHP application?

__________________

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-28-2007, 07:46 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
Quote:
Originally Posted by HelloWorld View Post
Intersting tutorial about the regular expression. But sorry if I sound dumb, what is the significant of knowing regular expression in PHP? I mean, how is that useful for our PHP application?
If you want to check if the username that they insert is validate?
or you want to check if the username provides you the right
email. You can check if the visa card number is in correct format.
And there are more other advantages. In part 3 I will show you
some useful Regular Expressions that you will use it in daily
application.

From siLenTz
Wait and see...

__________________

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 07:49 AM.
Reply With Quote
  #8 (permalink)  
Old 06-28-2007, 07:55 AM
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
user registration systems always use them, maybe you could do an example of what you would use in a user registration system?

__________________

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
  #9 (permalink)  
Old 06-28-2007, 08:39 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
In second part, I created a very simple program that check the validate
real name of users. If they insert the validate formate of real name then
we will add their name into database. If it is invalidate, we don't allow it
to pass through the database. In this program, I am using regular expression.

PHP Code:
   $regexp "^[A-Za-z][A-Za-z' -]$";
   if (
ereg($regexp$realname)) {
       
// Insert the realname into database
   
} else {
       
// Warn them to inser the correct realname
   

But if you don't know how to use regular expression, you probally do
it in the hard way. Here is raw php without using regular expression
and do exactly the same like the code above.

PHP Code:
<?php
    
function isAlphabet($char)
    {
        
// Check if ASCII of that character are between
        // 65 and 90 which is range value of ASCII for
        // character A to Z and check if it is between
        // the range of 97 to 122 because it is a-z
        // ASCII value.
        
if ((64 ord($char)) && (ord($char) < 91)) {
            return 
true;
        } elseif ((
96 ord($char)) && (ord($char) < 123)) {
            return 
true;
        }

        
// If none of above conditions are true
        // which mean that it is not an alphabet.
        
return false;
    }

    function 
checkUsername($str_username)
    {
        
// Check each character of the string
        
for($i 0$i strlen($str_username); $i++) {
            
$str_sub substr($str_username$i1);

            
// This first character must be alphabet.
            
if(!isAlphabet($str_sub) && ($i == 0))
                return 
false;

            
// If character isn't one of alphabet or
            // our allowed special character then it is
            // not a vaildate username.
            
if (!isAlphabet($str_sub) && !($str_sub==" ")
                && !(
$str_sub=="'") && !($str_sub=="-")) {
                return 
false;
            }
        }
        return 
true;
    }

    
$username "Devi Jonh";

    if (
checkUsername($username)) {
        print 
"validate";
    } else {
        print 
"invalidate";
    }
?>
If you don't understand about the regular expression i am using on the
first program, you should read the first and second part. Here
is the link to second part:
Regular Expressions In PHP (Part 2)

From siLenTz
Which one you prefer...??

__________________

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 01:01 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