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 php uses regularities to replace strings

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

Share

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

This article will explain in detail how php uses regularities to replace strings. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Php uses regular methods to replace strings: 1, using the preg_replace () function, syntax "preg_replace (regular expression, replacement value, string)"; 2, using preg_filter () function, syntax "preg_filter (regular expression, replacement value, string)".

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

There are two ways in which php replaces strings with rules:

Preg_replace ()

Preg_filter ()

Both the preg_replace () and preg_filter () functions can search and replace regular expressions, except that the preg_filter () function returns only the results of a successful match, while preg_replace () returns all the results, regardless of whether the match is successful or not.

The syntax of the preg_replace () and preg_filter () functions is similar:

Preg_replace ($pattern, $replacement, $subject [, $limit =-1 [, & $count]]) preg_filter ($pattern, $replacement, $subject [, $limit =-1 [, & $count]])

Search for the part of $subject that matches $pattern and replace it with $replacement.

The parameters are described as follows:

$pattern: the pattern to search for, you can make a string or an array of strings

Replacement: the string or array of strings used for replacement. If this parameter is a string and $pattern is an array, then all patterns are replaced with this string. If both $pattern and $replacement are arrays, each $pattern is replaced with the corresponding element in $replacement. If there are fewer elements in $replacement than in $pattern, the extra $pattern is replaced with an empty string.

$subject: the string or array of strings to search for and replace, if $subject is an array, search and replace back on each element of $subject, and the return value will also be an array.

$limit: optional parameter, the maximum number of times each mode can be replaced on each $subject. The default is-1 (infinite).

$count: optional parameter, if specified, will be populated with the number of completed replacements.

Example:

Preg_filter () and preg_replace () replace strings with regularities

This is the end of the article on "how php uses rules to replace strings". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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