PHP Regular Expression

Code for Matching a string in the text using preg_match function

 
      <?php
  #preg_match function for matching patterns 
  $pattern ="/satish/";
  echo preg_match($pattern,"this is written by satish");
  //returns 1 if a match is found or else returns 0 
  ?>
    
Output is 1

Code for Matching a string in the text using preg_match_all function

 
      <?php
  #preg_match function for matching patterns 
  $pattern ="/satish/";
  echo preg_match_all($pattern,"this is written by satish so the author is satish",$matches);
  #returns the number of occurrences or else returns 0. 
  #$matches is used for populating an array containing the matches 
  ?>
    
Output is 2

Code for demonstrating the use of preg_replace function

 
      <?php
  #preg_replace function for matching patterns 
  $pattern ="/Satish/";
  echo preg_replace($pattern,"Ramesh","Satish is teaching good");
  #returns the String after the replace 
  #if no match found then returns the string
  ?>
    
Output- Ramesh is teaching good

check if the string starts with S, M or R and is followed by atish ,Satish, Matish or Ratish is accepted.

 
      <?php
      $pattern = "/[SMR]atish/";
      if(preg_match($pattern,"Satish is a bad teacher"))
      {
          echo "pattern found";
      }else
      {
          echo "Pattern not found";
      }
      ?>
      
      we can also use this
    
      <?php
      $pattern = "/Matish|Satish|Ratish";
      if(preg_match($pattern,"We understand Matish is a bad teacher"))
      {
          echo "pattern found";
      }else
      {
          echo "Pattern not found";
      }
      ?>
    
Output

Code to demonstrate word boundary

 
      <?php
  $pattern = "/day\b/";
  if(preg_match($pattern,"it is what we do during daytime"))
  {
      echo "Match is found";
  }else
  {
      echo "Match is not found";
  }
  ?>
    
Output - match is found

Code for checking if a pattern occurs in the starting of the word

 
      <?php
      $pattern = "/\bday/";
      if(preg_match($pattern,"it is what we do every daytime"))
      {
          echo "Match is found";
      }else
      {
          echo "Match is not found";
      }
      ?>
      
    
Output- match is found

Code to check if a username starts with a digit [0-9].Alert if it starts with a digit

 
      <?php
  $pattern = "/^[^0-9][A-Za-z]+/";
  if(preg_match($pattern,"23Satish"))
  {
      echo "Username is fine";
  }else
  {
      echo "Username should not start with a digit";
  }
  ?>
    
Output

Code to check if username contains spaces. If it contains spaces then alert spaces not allowed

 
      <?php
  $pattern = "/\s/";
  if(preg_match($pattern,"SatishCJ"))
  {
      echo "Username should not contain spaces";
  }else
  {
      echo "Username is fine";
  }
  ?>
    
Output

Ensure Username should start with a character and should be 8 digits minimum in length Username should not contain any spaces using PHP regular expressions

 
      <?php
      $pattern = "/^[A-Za-z][A-Za-z]{7,}[^\s]/";
      if(preg_match($pattern,"SatishCJsatish"))
      {
          echo "Username is fine";
      }else
      {
          echo "Username is not matching the criteria";
      }
      ?>
    
Output

Code to check if registration number matches the pattern ddBCEdddd

 
      <?php
  $pattern = "/\d\dBCE\d\d\d\d/";
  if(preg_match($pattern,"12BKT2001"))
  {
      echo "Registration Number is fine";
  }else
  {
      echo "Registration number is not matching the criteria";
  }
  ?>
    
Output

Code for password validation in PHP using Regular Expressions

 
      <?php
  $pattern = "/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W).{8,}/";
  if(preg_match($pattern,"satishCJ12#456"))
  {
      echo "Password is Good";
  }else
  {
      echo "Password is not matching the criteria";
  }
  ?>
    
Output