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

How to use preg_match_all

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to use preg_match_all". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The copy code is as follows:

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

Search subject for everything that matches the regular expression given by pattern and put the results into matches in the order specified by flags.

After the first match is searched, the next search starts at the end of the previous match.

Pay special attention to PREG_PATTERN_ORDER and PREG_SET_ORDER

Flags can be a combination of the following tags (note that it doesn't make sense to combine PREG_PATTERN_ORDER and PREG_SET_ORDER):

If you use PREG_PATTERN_ORDER

Sort the results so that $matches [0] is an array of all patterns, $matches [1] is an array of strings matched by the subpatterns in the first parenthesis, and so on. (that is, $matches [0] [0] is each item in all pattern matches, $matches [0] [1] is the second item in all pattern matches, $matches [1] [0] is the first item in every parenthesis, and $matches [1] [0] is the second item in every parenthesis)

The copy code is as follows:

This example outputs:

The copy code is as follows:

Example:, this is a test

Example:, this is a test

Therefore, $out [0] contains a string that matches the entire pattern, and $out [1] contains a string between a pair of HTML tags.

If you use PREG_SET_ORDER

Sort the results so 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. (that is, $matches [0] [0] is the fully matched string in the first set of matches, and $matches [0] [1] is the fully matched string in the first parenthesis in the first set of matches)

The copy code is as follows

This example outputs:

The copy code is as follows:

Example:, example:

This is a test, this is a test

In this case, $matches [0] is the first set of matching results, $matches [0] [0] contains text that matches the entire pattern, $matches [0] [1] contains text that matches the first subpattern, and so on. Again, $matches [1] is the second set of matching results, and so on.

PREG_OFFSET_CAPTURE

If this flag is set, the string offset associated with each matching result is also returned. Notice that this changes the value of the returned array so that each cell is also an array, where the first item is the matching string and the second item is its offset in subject. This tag has been available since PHP 4.3.0.

If no tag is given, it is assumed to be PREG_PATTERN_ORDER.

Returns the number of times the entire pattern matches (possibly zero), or FALSE if an error occurs.

Example 1. Get all the phone numbers from a text

The copy code is as follows:

Example 2. Search for matching HTML tags (greedy)

The copy code is as follows:

This example outputs:

The copy code is as follows:

Matched: bold text

Part 1:

Part 2: bold text

Part 3:

Matched: click me

Part 1:

Part 2: click me

Part 3:

This is the end of "how to use preg_match_all". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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