Friday, October 24, 2025
HomeLanguagesPHP ereg() Function

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:

  • pattern: It is a regular case-sensitive expression.
  • str: It is the input string.
  • arr: It’s an optional input parameter that contains an array of all matched expressions that were grouped by parentheses in the regular expression.

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




<?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




<?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




<?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

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS