POSIX extended regular expression functions

巴扎黑
Release: 2023-03-07 16:24:02
Original
1996 people have browsed it

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 = &#39;^[$][[:alpha:]__][[:alnum:]]*&#39;;
ereg($ereg,&#39;$_name&#39;,$register);
var_dump($register);
?>
Copy after login

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 = &#39;tm&#39;;
$str = &#39;hello ,tm,Tm,tM&#39;;
$rep_str = eregi_replace($ereg,&#39;TM&#39;,$str);
echo $rep_str;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!