finding strings is the primary application of regular expressions. In PHP, the two functions that are available and used to match POSIX-style regular expressions are the Ereg () function and the eregi () function.
Ereg () function and eregi () function
The function syntax is formatted as follows:
int Ereg (string pattern,string search,array[matches]);
function function: This function searches for string search and looks for a string that matches the regular expression in pattern. If a string that matches the pattern's subexpression is found, the strings will be stored in the array matches, and each array element corresponds to a sub-expression.
The function eregi () function is the same as the Ereg () function except that it is case insensitive.
The example demonstrates using the Ereg () function to verify that a variable is legitimate:
<?phpheader ("Content-type:text/html;charset=utf-8"); $ereg = ' ^[$][[:alpha:]__][[:alnum:]]* '; Ereg ($ereg, ' $_ Name ', $register); Var_dump ($register);? >
Note: In the new version of PHP, Ereg has been deprecated and replaced by Preg_match ().
Ereg_replace () and Eregi_replace ()
The function syntax is formatted as follows:
String Ereg_replace/ereg_replace (string pattern, string replacement, string string)
function function: Matches the expression pattern in the character-swapping string, and if the match succeeds, replaces the matching string with replacement and returns the replaced string. If no match is found in the string, the string is returned as-is. Eregi_replace () is case insensitive.
The instance shows that all the non-uppercase TMS of the string are converted to uppercase TM:
<?phpheader ("Content-type:text/html;charset=utf-8"); $ereg = ' TM '; $str = ' Hello, tm,tm,tm '; $rep _str = Eregi_replace ($ereg, ' TM ', $str); Echo $rep _str;
Note: In the new version, Eregi_replace () is replaced by Preg_replace ().