Basic PHP Validations with regular expression

Basic PHP Validations with regular expression

This post explains about regular expression in PHP.

Regular expressions were originally designed to help working with strings under Unix systems.

Using regular expressions you can easily  find a pattern in a string and/or replace it if you want. This is a very powerful tool in your hand, but be careful as it is slower than the standard string manipulation functions.

Regular expression types:- 

There are 2 types of regular expressions:

  • POSIX Extended
  • Perl Compatible

The ereg, eregi etc are the POSIX versions and preg_match, preg_replace etc are the Perl version. Perl Compatible version is more powerful and faster as well than the POSIX one. Using Perl compatible regular expressions the expression should be enclosed in the delimiters, a forward slash (/).

First of all, let's take a look at two special symbols: '^' and '$'.  
They indicate the start and the end of a string, like this:

"^xyz": matches any string that starts with "xyz". eg:- xyza, xyz b etc
"xyz$": matches a string that ends in the substring "xyz". eg:- axyz,
a xyz etc
"^xyz$": a string that starts and ends with "xyz". eg:- axyzb, a xyz b
etc
There are also the symbols '*', '+', and '?', which describe the
number of times a characters may occur. They indicate "zero or more",
"one or more", and "zero or one".

"xyz*": matches a string that has followed by zero or more z's.
eg:- xy,   xyzzzz etc
"xyz+": same, but there's at least one z. eg:- xyz,  xyzzzz etc
"xyz?": there might be one z or not. eg:- xyz,  xy etc
Using braces you can indicate ranges in the number of occurences.

"xyz{2}": matches a string that has an a followed by exactly two z's.
eg:- xyzz
"xyz{2,}": there are at least two z's. eg:- xyzz, xyzzzz etc
"xyz{3,5}": from three to five z's. eg:- xyzzz, xyzzzz and xyzzzz
Using parentheses you can indicate ranges in the sequence of characters

"x(yz)+": matches a string that has an a followed by at least one 
copies of the sequence "yz". eg:- xyz, xyzyzyz
There are also the '|' symbol, which indicate  OR operator

"x|y": matches a string that has either "x" or "y" in it.
eg:- wxz, yza etc
"(x|y)*z": matches a string that has a sequence of alternating x and y
ending in z.  eg:- wxz, ayz
Using Bracket you can indicate which characters are allowed in a single
position of a string

"^[a-zA-Z]": matches a string that starts with a letter.
eg:- Vishal, PHP, Jquery,ajax etc

There are also (.), which indicate  any single character

[0-9\.\-]:- matches any number, dot, or minus sign eg:- 987 etc


Example 

 

 Valid Text:- 
      function validText($str){
      $regExp="/^[0-9a-zA-Z\s]+$/";    
      /*Means  any alphanumeric string with space allowed  
      eg:- 'Vishal Gupta', 'Vishal ', 'Vishal 00001' etc*/
      if(preg_match($regExp,trim($str))){  
     /*If string is valid string according to regular
      expression than return true, else return false*/
            return true;
      }else{
            return false;
      }
}
 Valid Mobile Number:-
      function mobileNumber($str){
      $regExp="/^[0-9]{10}+$/";   /*Means  number with exact count 10
      eg:- '8146111782', '1234567890' etc*/
      if(preg_match($regExp,trim($str))){  
     /*If mobile number is valid mobile number according to regular 
         expression than return true, else return false*/
          return true;
      }else{
          return false;
      }
}
 Valid Email Id:- 
      function validEmailId($strEmailId){
      $regExp =
       /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
      /*Means  email id start with any alphanumeric character with
      underscore symbol ('_'), hyphen symbol ('-') and dot symbol ('.')
      and after that there must be a at symbols ('@') and than again 
      any alphanumeric character with underscore symbol ('_'), hyphen 
      symbol ('-') and dot symbol ('.') and after that a dot symbol
      ('.')  and than any letter with only count 2 to 5.  
      eg:- info@91weblessons.com, vishalgupta.rgec@gmail.com etc */
      if(preg_match($regExp,trim($strEmailId))){
          return true;
      }else{
          return false;
      }
}
Both comments and pings are currently closed.

5 Responses to “Basic PHP Validations with regular expression”

  1. Great article. Keep up the good work.

  2. It seems to be like great put up, on the other hand it just a single side on the medal. Good reading anyway, I constantly appreciated great brain teaser and solid amount of pleasant information.

  3. Surprisingly challenging many thanks, I do believe your trusty subscribers could perhaps want even more stories like this keep up the fantastic perform.

  4. Saved as a favorite, I really like your web site!

  5. It’s truly a nice and helpful piece of information.

    I am satisfied that you just shared this helpful
    info with us. Please keep us informed like this. Thank you for
    sharing.

Domains @ Rs.99