


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
|

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools