


Example understanding of preg_replace() parameters are multiple replacements of arrays_PHP tutorial
This article introduces to students some solutions to self-understanding and replacement times when the preg_replace() parameters are array replacements multiple times. If you need to know more, please do not refer to it.
mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
pattern
The pattern to search for. Can be a string or an array of strings.
A number of PCRE modifiers, including 'e' (PREG_REPLACE_EVAL), can be specified for this function.
replacement
The string or array of strings used for replacement. If this parameter is a string, and pattern is an array, then all patterns use this string for replacement. If pattern and replacement are both arrays, each pattern uses replacement Replace the corresponding elements in replacement. If there are fewer elements in replacement than in pattern, the extra pattern will be replaced with an empty string.
Replacement can contain backreferences n or (available from PHP 4.0.4 and above) $n, the latter is syntactically preferred. Each such reference will be replaced by the text captured by the matching nth capture subgroup . n can be 0-99, � and $0 represent the complete pattern matching text. The serial number counting method of the capturing subgroup is: the left bracket representing the capturing subgroup is counted from left to right, starting from 1. If you want to use it in replacement Four backslashes must be used ("", annotation: because this is first a PHP string, after escaping, there are two, and then it is considered to be one original backslash after being passed through the regular expression engine).
When working in replacement mode and the backreference needs to be followed by another number (for example: adding a source number immediately after a match pattern), you cannot use the syntax 1 to describe the backreference. For example, 11 will cause preg_replace() to not understand whether you want a 1 backreference followed by an original 1, or an 11 backreference followed by nothing. The solution in this case is to use ${1} 1. This creates an independent $1 backreference, an independent original text 1.
When using the e modifier, this function will escape some characters (i.e.:', ", and NULL) and then perform backreference replacement. When this is done, please make sure that there are no single quotes or single quotes after the backreference is resolved. Syntax errors caused by double quotes (for example: 'strlen('$1')+strlen("$2")'). Make sure to comply with PHP's string syntax and comply with eval syntax. Because after completing the replacement, the engine will replace the resulting characters The string is evaluated as a php code using the eval method and the return value is used as the string that ultimately participates in the replacement.
subject
The string or array of strings to search and replace.
If the subject is an array, search and replacement will be performed on each element of the subject, and the return value will also be an array.
limit
The maximum number of substitutions per subject per pattern. Default is -1 (unlimited).
count
If specified, will be filled with the number of completed substitutions.
Yes - a function that performs a regular expression search and replace. Usually when we use it, it usually ends with one substitution. Today I looked at the PHP manual and found a relatively difficult to understand (I think) example to share with you.
The code is as follows | Copy code | ||||
|
The results are as follows:
At first glance, I was dizzy
Generally, if the matching pattern and replacement content are both arrays, they should correspond. If there are fewer elements in replacement than in pattern, the extra pattern will be replaced with an empty string.
$pattern is like a scanner. If it scans a match, it will be replaced with the corresponding $replace
The replacement process for the above example is as follows
/d/Scan 1 in $subject and it matches. If the matching content is $0 (that is, 1), replace 1 with A:1
Then use /[a-z]/ to scan A:1 if it does not match, and do not replace it. Continue to use [1a] to scan A:1. If the matching content is 1 (that is, $0), replace the 1 in A:1 with C:1
The first item was eventually replaced with A:C:1
Simplify the process
1->A:1->A:C:1
a->B:a->B:C:a
2->A:2
b->B:b
A (If there is no match, it will not be replaced)
B(same as above)
4->A:4
Take each pattern in $pattern to match each element in $subject in turn. When matched, replace it with the $replace corresponding to $pattern. As in the above example, it may be replaced more than once
Control preg_replace replacement times
The code is as follows | Copy code | ||||
$str=preg_replace("/$v[0]/","".$v[0]."",$str,2); //bb,cc,abcde,www.bkjia.com, bb
|

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The method of using a foreach loop to remove duplicate elements from a PHP array is as follows: traverse the array, and if the element already exists and the current position is not the first occurrence, delete it. For example, if there are duplicate records in the database query results, you can use this method to remove them and obtain results without duplicate records.

Methods for deep copying arrays in PHP include: JSON encoding and decoding using json_decode and json_encode. Use array_map and clone to make deep copies of keys and values. Use serialize and unserialize for serialization and deserialization.

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values takes a relatively long time.

Multidimensional array sorting can be divided into single column sorting and nested sorting. Single column sorting can use the array_multisort() function to sort by columns; nested sorting requires a recursive function to traverse the array and sort it. Practical cases include sorting by product name and compound sorting by sales volume and price.

The best practice for performing an array deep copy in PHP is to use json_decode(json_encode($arr)) to convert the array to a JSON string and then convert it back to an array. Use unserialize(serialize($arr)) to serialize the array to a string and then deserialize it to a new array. Use the RecursiveIteratorIterator to recursively traverse multidimensional arrays.

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

A fast score query tool provides students and parents with more convenience. With the development of the Internet, more and more educational institutions and schools have begun to provide online score check services. To allow you to easily keep track of your child's academic progress, this article will introduce several commonly used online score checking platforms. 1. Convenience - Parents can check their children's test scores anytime and anywhere through the online score checking platform. Parents can conveniently check their children's test scores at any time by logging in to the corresponding online score checking platform on a computer or mobile phone. As long as there is an Internet connection, whether at work or when going out, parents can keep abreast of their children's learning status and provide targeted guidance and help to their children. 2. Multiple functions - in addition to score query, it also provides information such as course schedules and exam arrangements. Many online searches are available.

2024 is the first year of AI mobile phones. More and more mobile phones integrate multiple AI functions. Empowered by AI smart technology, our mobile phones can be used more efficiently and conveniently. Recently, the Galaxy S24 series released at the beginning of the year has once again improved its generative AI experience. Let’s take a look at the detailed function introduction below. 1. Generative AI deeply empowers Samsung Galaxy S24 series, which is empowered by Galaxy AI and brings many intelligent applications. These functions are deeply integrated with Samsung One UI6.1, allowing users to have a convenient intelligent experience at any time, significantly improving the performance of mobile phones. Efficiency and convenience of use. The instant search function pioneered by the Galaxy S24 series is one of the highlights. Users only need to press and hold
