Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand php preg_replace replacement

Shulou Source: shulou.com Published: 2022-06-03 07:40:16 09月14日 Update

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!

Tags: Content code learning that is elements patterns processes examples functions as above ideas situations manuals scanners arrays articles more rules knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Redmi Shulou Technology Docker OPPO Reno