Monday 12 March 2012

PHP Regular Expression Function

  • Regular expression means the formula or the general format of any string or expression.
  • It is used for pattern matching.
  • It is also known as regexis.           
  • It checks any characters or digits which are specified in the format.
  • It is used generally for the validation on server side scripting.

There are two type of regular expression

[1]POSIX Extended regular expression
[2]Perl Compatible regular expression

Ø       [1] POSIX regular expression

When this type of regular expression is used then the function ereg() is required to be used for the pattern matching. The function ereg() is abbreviated as extended regular expression. It is oldest type of regular expression in use.

There are different symbols used which are given below.

Symbol
Description
^
For starting the format string.
$
For ending the format string.
[a-z]
Only a lower case alphabets.
[A-Z]
Only a upper case alphabets.
[a-z A-z]
Only alphabets in any form.
[0-9]
Only digits.
[a-z A-Z 0-9]
Alphanumeric character.
\
Used when any special character are to be used in the string.
()
Used when more than one value is to be used.
|
For or condition
{n}
Is used when the lengths of the digit are known. Where n is an integer value.
{n,m}
N for the minimum value and m for the maximum value.
*
One or more times.
+
Zero or more times.
?
Zero or one times.

When the format which is matched is not found then it returns false otherwise it returns true.

Syntax for ereg()

Boolean ereg(format , variable or value to be checked)

Example

<?php 
          $date = "01/01/2011";
          if(ereg('^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$',$date))
          {
                   echo "It is valid date";
          }
          else
          {
                   echo "It is Not valid date";
          }
?>

Ø       [2] Perl Compatible Regular Expression

This type of regular expression is developed using the perl language. The function which is used for the given regular expression is preg_match() that is Perl regular expression.

It returns the Boolean value. If the format is matched, then it returns true value otherwise it return false value.

There are different symbols used which are given as below.

Symbol
Description
\d
Any number.
\D
For Anything other than a number
\s
Any kind of whitespace
\S
Anything other than whitespace.
\W
Any word character(including the underscore character)
\w
Anything other than a word character.
\A
Beginning of string
\b
Word boundary
\B
Not a word boundary
\Z
End of string(matches before final new line or at the end of string)
\z
End of string(matches only at the very end of the string)

Syntax for ereg()

Boolean preg_match(format , variable or value to be checked)

Example

<?php     
     $date = "1234567891";
     if(preg_match('/\+91\d{10}/',$date))
     {
              echo "It is valid Mobile number";
     }
     else
     {
              echo "It is Not valid Mobile number";
     }
?>

Posted By : Hemangi Zala

No comments:

Post a Comment