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 understand php preg_replace replacement

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to understand php preg_replace replacement". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand php preg_replace replacement".

The copy code is as follows:

Mixed preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit =-1 [, int & $count]])

Is to perform a regular expression search and replacement with the function, usually we use it is a replacement end, today look at the php manual found a more difficult to understand (think) example to share with you.

The copy code is as follows:

The results are as follows:

At first glance, I was dizzy.

Generally speaking, if the matching pattern and replacement content are arrays, they should correspond to each other. If there are fewer elements in replacement than in pattern, the extra pattern should be replaced with an empty string.

$pattern is like a scanner, scan the matching and use it for the corresponding $replace replacement.

For the above example, the replacement process is as follows:

/\ d / scan the 1 in $subject and match it. If the match is $0 (that is, 1), replace 1 with ARV 1.

Then use / [amurz] / scan ARV 1 does not match, do not replace it, continue to scan ARV 1 with [1a], and match 1 (that is, $0), then replace ARV 1 with CRV 1.

The first item was eventually replaced with A:C:1

Simplify the process:

1-> Avatar 1-> A:C:1

A-> Bvana-> B:C:a

2-> Apur2

B-> BRV b

A (if there is no match, it will not be replaced)

B (ibid.)

4-> AVR 4

To sum up, take each pattern in $pattern to match every element in $subject in turn, and then use the $replace exchange corresponding to $pattern. As in the example above, it may be replaced more than once.

Thank you for your reading, the above is the content of "how to understand php preg_replace replacement". After the study of this article, I believe you have a deeper understanding of how to understand php preg_replace replacement, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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