Finding strings is the main application of regular expressions. In PHP, two functions available for matching POSIX-style regular expressions are the ereg() function and the eregi() function.
ereg() function and eregi() function
The function syntax format is as follows:
int ereg( string pattern,string search,array[matches]);
Function function: This function searches the string search and finds a string in pattern that matches the regular expression. If strings are found that match the subexpressions of pattern, these strings will be stored in the matches array, with each array element corresponding to one subexpression.
Function The eregi() function has the same functions as the ereg() function except that it is not case-sensitive.
An example shows using the ereg() function to verify whether the variable is legal:
<?php header("content-type:text/html;charset=utf-8"); $ereg = '^[$][[:alpha:]__][[:alnum:]]*'; ereg($ereg,'$_name',$register); var_dump($register); ?>
Note: ereg has been deprecated in the new version of PHP and replaced by preg_match() .
ereg_replace() and eregi_replace()
The function syntax format is as follows:
string ereg_replace/ereg_replace(string pattern , string replacement, string string)
Function function: Match the expression pattern in the character replacement string. If the match is successful, use replacement to replace the matching string and return the replaced string. If no match is found in string, string will be returned unchanged. eregi_replace() is not case sensitive.
Example shows how to replace all non-uppercase TMs in the string with uppercase TMs:
<?php header("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().
The above is the detailed content of POSIX extended regular expression functions. For more information, please follow other related articles on the PHP Chinese website!