Detailed explanation of commonly used functions in PHP regular expressions

黄舟
Release: 2023-03-17 11:08:02
Original
1757 people have browsed it

In the previous articles, we learned the use ofphp regular expressionsand getting started. Today we will focus on introducing thecommon functions of php regular expressions, both It can be used perfectly when combined! !

1. preg_match()

Function prototype:

int preg_match (string $pattern, string $content [, array $matches])
Copy after login

preg_match () function searches the $content string for content that matches the regular expression given by $pattern. If $matches is provided, the matching results are placed in it. $matches[0] will contain the text that matches the entire pattern, $matches[1] will contain the first captured match of the pattern element enclosed in parentheses, and so on. This function only performs one match and ultimately returns the number of matching results of 0 or 1. Listing 6.1 shows a code example for the preg_match() function.
Code 6.1 Date and time matching
The code is as follows:

Copy after login

This is a simple dynamic text string matching example. Assuming that the current system time is "13:25 on August 17, 2006", the following content will be output.
The matching time is: 2006-08-17 01:25 pm
The current date is: 2006-08-17
The current time is: 01:25 pm

2 . ereg() and eregi()

ereg() is the matching function for regular expressions in the POSIX extension library. eregi() is a case-ignoring version of the ereg() function. Both have similar functions to preg_match, but the function returns a Boolean value indicating whether the match was successful or not. It should be noted that the first parameter of the POSIX extension library function accepts a regular expression string, that is, no delimiter is required. For example, Listing 6.2 is a method for checking the security of file names.
Code 6.2 Security check of file name
The code is as follows:


##

Copy after login


Normally, use The Perl-compatible regular expression matching function perg_match() will be faster than using ereg() or eregi(). If you just want to find whether a string contains a certain substring, it is recommended to use the strstr() or strpos() function.

Replacement of regular expressions

1. ereg_replace() and eregi_replace()

Function prototype:

string ereg_replace (string $pattern, string $replacement, string $string) string eregi_replace (string $pattern, string $replacement, string $string)
Copy after login

ereg_replace() searches for the pattern string $pattern in $string and replaces the matched result with $replacement. When $pattern contains pattern units (or sub-patterns), positions in $replacement such as "/1" or "$1" will be replaced by the content matched by these sub-patterns. And "/0" or "$0" refers to the content of the entire matching string. It should be noted that the backslash is used as an escape character in double quotes, so the form "//0" and "//1" must be used.

eregi_replace() and ereg_replace() have the same functions, except that the former ignores case. Code 6.6 is an application example of this function. This code demonstrates how to do simple cleaning work on the program source code.
Code 6.6 Cleaning up the source code
The code is as follows:

Copy after login


2. preg_replace()

Function prototype:

mixed preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit])
Copy after login

preg_replace is more powerful than ereg_replace. The first three parameters can all use arrays; the fourth parameter $limit can set the number of replacements, and the default is to replace all. Code 6.7 is an application example of array replacement.

Code 6.7 Array replacement
The code is as follows:


/nEmail: {Email}
/nAddress: {Address}
/n"; //模式 $patterns =array( "/{Address}/", "/{Name}/", "/{Email}/" ); //替换字串 $replacements = array ( "No.5, Wilson St., New York, U.S.A", "Thomas Ching", "tom@emailaddress.com", ); //输出模式替换结果 print preg_replace($patterns, $replacements, $string); ?>
Copy after login

The output results are as follows.

Name: Thomas Ching", Email: tom@emailaddress.com Address: No.5, Wilson St., New York, U.S.A
Copy after login

The pattern modifier "e" can be used in the regular expression of preg_replace. Its function is to use the matching result as an expression and can be re-operated. For example:

The code is as follows:

TEST

My Picture”; //输出结果中HTML标签将全部为小写字母 echo preg_replace ( "/(]*>)/e", "'//1'.strtolower('//2').'//3'", //此处的模式变量//2将被strtolower转换为小写字符 $html_body); ?>
Copy after login

Summary:

preg_replace function uses Perl-compatible regular expression syntax, which is usually better than ereg_replace Faster alternative. If you only want to do a simple replacement of a string, you can use the

str_replacefunction.

Related recommendations:

Detailed explanation of preg_match_all function in php regular expressions


Detailed explanation of preg_match function in php regular expression


A case of php regular expression to verify email address


Instance analysis of php regular expressions


Detailed introduction to php regular expressions

The above is the detailed content of Detailed explanation of commonly used functions in PHP regular expressions. 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
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!