php preg_match() code example

Example 1: preg_match in php

<?php
//Syntex : int preg_match( $pattern, $input, $matches, $flags, $offset)
  
// Declare a variable and initialize it 
$str = "Check For Testing."; 
  
// case-Insensitive search for the word "Check" 
if (preg_match("/\bCheck\b/i", $str, $match))  
    echo "Matched!"; 
else
    echo "not matched";   
  

// Output : Matched
?>

Example 2: preg_match in php

<?php
$my_email = "[email protected]";
if (preg_match("/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/", $my_email)) {
echo "$my_email is a valid email address";
}
else
{
  echo "$my_email is NOT a valid email address";
}
?>

Example 3: preg_match in php

<?php
   $line = "Vi is the greatest word processor ever created!";
   // perform a case-Insensitive search for the word "Vi"
   
   if (preg_match("/\bVi\b/i", $line, $match)) :
      print "Match found!";
      endif;
?>

Example 4: php preg_match

preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] ) : int

Example 5: preg_match in php

int preg_match (string pattern, string string [, array pattern_array], [, int $flags [, int $offset]]]);

Tags:

Php Example