Open In App

PHP ereg() Function

The Ereg() function in PHP searches a string to match the regular expression given in the pattern. The function is case-sensitive. This function has been deprecated in PHP 5.3.0 and removed in PHP 7.0.0.

Syntax:



int ereg ( string $pattern , string $str, array &$arr );

Parameters:

Return value:



If the pattern is found, the function will return true else false. It returns the length of the matched string if the pattern match was found in the string, or false if no match was found or an error occurred.  The function returns 1 if the optional parameter arr has not been passed or the length of the matched string is 0.

Example 1:  In this example, the statement checks whether the subject provided to ereg() function contains .org or not.




<?php
  
 echo ereg("(\.)(org$)", "www.geeksforgeeks.org");
  
?>

Output: 

1

Example 2: This example examines whether the subject begins with ‘g’ or not. The ‘^’ symbol is used to check if the subject starts with the required string.




<?php
  
echo ereg("g","gfg");  
  
?>

Output: 

1

Example 3: In this example, the following code snippet will take the date in DD-MM-YYYY format and print it in ISO format (YYYY-MM-DD).




<?php
  
if (ereg ("([0-9]{1,2})-([0-9]{1,2})-([0-9]{4})",
            "10-12-1999", $arr)) {
  
   echo "$arr[3]-$arr[2]-$arr[1]";
  
} else {
  
   echo "Invalid date format entered";
  
}
  
?>

Output: 

1999-12-10


Article Tags :