POSIX extended Regular expression functions

Source: Internet
Author: User
Tags ereg posix
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 ().

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.