Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of preg_match and preg_match_all functions in php

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Xiaobian to share with you what is the use of preg_match and preg_match_all functions in php, I hope you have gained something after reading this article, let's discuss it together!

Application of regular expressions in PHP

In PHP applications, regular expressions are mainly used for:

·Regular matching: matching corresponding content according to regular expressions

·Regular replacement: Match content and replace based on regular expressions

·Regular Split: Split strings according to regular expressions

There are two types of regular expression functions in PHP, Perl-compatible regular expression functions and POSIX extended regular expression functions. There is little difference between the two, and Perl-compatible regular expression functions are recommended, so the following are examples of Perl-compatible regular expression functions.

delimiter

Perl-compatible regular expression functions whose regular expressions need to be written in delimiters. Any character that is not a letter, number, or backslash () can be used as a delimiter. Usually we use/as a delimiter. See the following examples for specific uses.

prompt

Although regular expressions are very powerful, if you can do it with ordinary string processing functions, try not to use regular expression functions because regular expressions are much less efficient. About common string processing functions.

preg_match()

The preg_match() function is used to match regular expressions and returns 1 if successful, or 0 otherwise.

Grammar:

int preg_match( string pattern, string subject [, array matches ] )

Parameter Description:

parameter description pattern regular expression subjectObjects to match retrieved matches optional, array storing match results,$matches[0] will contain text matching the entire pattern,$matches[1] will contain text matching the subpattern in the first captured parenthesis, and so on

Example 1:

The copy code is as follows:

Browser output:

The copy code is as follows:

A match was found: PHP

In this example, php is matched case-insensitive to text because of the i modifier.

prompt

preg_match() stops matching after the first successful match. If you want to match all results, that is, search to the end of subject, you need to use the preg_match_all() function.

Example 2: Get the host domain name from a URL:

The copy code is as follows:

Browser output:

The copy code is as follows:

Domain name: jb51.net

preg_match_all()

The preg_match_all() function is used to perform global matching of regular expressions, returning the number of times the entire pattern matches successfully (possibly zero), and returning FALSE if there is an error.

Grammar:

int preg_match_all( string pattern, string subject, array matches [, int flags ] )

Parameter Description:

parameter description pattern regular expression subject objects to be retrieved matches array flags to store matching results

Optional, specifies the order in which the matches are placed in matches. Available tags are:

PREG_PATTERN_ORDER: By default, sort results such that $matches[0] is an array of all pattern matches,$matches[1] is an array of strings matched by the subpattern in the first parenthesis, and so on

PREG_SET_ORDER: Sorts the results such that $matches[0] is the array of the first set of matches,$matches[1] is the array of the second set of matches, and so on

PREG_OFFSET_CAPTURE: If this flag is set, the string offset attached to each occurrence of the match is also returned

The following example demonstrates displaying keywords (php) in all tags in text in red.

The copy code is as follows:

Regular Matching of Chinese Characters

Regular matching Chinese characters differ slightly according to different page codes:

GBK/GB2312 code: [x80-xff>]+ or [xa1-xff]+

UTF-8 code: [x{4e00}-x{9fa5}]+/u

Examples:

The copy code is as follows:

Output:

The code is as follows:

Array

(

[0] => Array

(

[0]=> Learning

[1]>> is a happy thing.

)

)

After reading this article, I believe you have a certain understanding of "what is the use of preg_match and preg_match_all functions in php". If you want to know more related knowledge, welcome to pay attention to the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report