
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
Preg_replace_callback_array() in PHP 7
Preg_replace_callback_array() function in PHP 7 represents a regular expression and replaces the use of callbacks. This function returns a string or an array of strings to match a set of regular expressions and replaces them using a callback function.
Syntax
preg_replace_callback_array(patterns, input, limit, count)
Parameter Values:
- pattern − It requires an associate array to associate the regular expression patterns to callback functions.
- input/subject −It requires an array of strings to perform replacements.
- limit −It is optional. -1 is used for default, which means it is unlimited. It sets a limit to how many replacements can be done in each string.
- count −It is also optional like the limit. This variable will contain a number, indicating how many replacements were performed after the function gets to execute.
- flags −It can be a combination of the preg_offset_captureandpreg_unmatched_as_null flags, which impact the format of the matched array.
- Return Values −preg_replace_callback_array() returns a string or an array of strings.If an error is found, then it will return a null value.If matches are found, the new subject will be returned, otherwise,the subject will be returned unchanged.
Preg_replace_callback_array() : Example
<html> <head> <title> PHP 7 Featuretutorialpoint:</title> </head> <body> <?php $subject = 'AaaaaaaBbbbCccc'; preg_replace_callback_array ( [ '~[a]+~i' => function ($match) { echo strlen($match[0]), ' number of "a" found', PHP_EOL; }, '~[b]+~i' => function ($match) { echo strlen($match[0]), ' number of "b" found', PHP_EOL; }, '~[c]+~i' => function ($match) { echo strlen($match[0]), ' number of "c" found', PHP_EOL; } ], $subject ); ?> </body> </html>
Output
The output for the above program code is −
7 number of "a" found 4 number of "b" found 5 number of "c" found
- Related Articles
- Display array structure and values in PHP 7
- Callback function in C
- Migrating PHP 5.x to PHP 7 on CentOS 7
- Anonymous classes in PHP 7?
- Generator delegation in PHP 7
- Type Hinting in PHP 7
- Constant arrays in PHP 7
- Group Use declarations in PHP 7
- Generator Return Expressions in PHP 7
- Exceptions and Error in PHP 7
- Uniform variable syntax in PHP 7
- What are callback functions in JavaScript?
- Callback using Interfaces in Java\n
- Explain the callback Promise.finally in JavaScript
- array() function in PHP

Advertisements