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.
int ereg ( string $pattern , string $str, array &$arr );
- 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.
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.
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.
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).