PHP developers sometimes need to apply several regular expression replacements with a separate callback for each pattern. This comes up when preparing data, displaying results, or integrating information with other services.
The preg_replace_callback_array() operation handles this particular task. The following examples show its result, its limitations, and a practical way to apply it.
What Is preg_replace_callback_array() in PHP?
Apply several regular expression replacements with a separate callback for each pattern.
Its syntax is:
preg_replace_callback_array(array $pattern, string|array $subject, int $limit = -1, int &$count = null, ?int $flags = null): string
The syntax shows the expected parameters and return type. Arguments marked with an ampersand are passed by reference and may be modified. Optional parameters control additional behavior; values must match the declared types.
Important: Callback patterns run in array order, so an earlier replacement can affect a later pattern. Always check for invalid patterns.
Parameters:
$pattern— A named input or configuration argument; use the declared type and optional default shown above.$subject— A named input or configuration argument; use the declared type and optional default shown above.$limit— A named input or configuration argument; use the declared type and optional default shown above.&$count— May be updated by the function.$flags— A named input or configuration argument; use the declared type and optional default shown above.
Example 1: Basic Usage
This first example uses a small input to demonstrate the main result of preg_replace_callback_array().
<?php
$rules = ["/\bcat\b/" => fn($matches) => strtoupper($matches[0]), "/\bdog\b/" => fn($matches) => "pet"]; echo preg_replace_callback_array($rules, "cat and dog");
Output:
CAT and pet
The output demonstrates the primary operation: apply several regular expression replacements with a separate callback for each pattern.
Example 2: Understanding Another Case
This second example changes the input or option to reveal a different behavior of preg_replace_callback_array().
<?php
$rules = ["/\d+/" => fn($matches) => "[" . $matches[0] . "]"]; echo preg_replace_callback_array($rules, "Order 12, line 3");
Output:
Order [12], line [3]
Compared with the first example, this one illustrates the same operation with a different input shape or option. Callback patterns run in array order, so an earlier replacement can affect a later pattern. Always check for invalid patterns.
Practical Example: A Real Application Scenario
Here is a practical use of preg_replace_callback_array() with realistic sample information. Adapt the values to match your application.
<?php
$rules = ["/\bERROR\b/" => fn($matches) => "[error]", "/\bWARN\b/" => fn($matches) => "[warning]"]; $logEntry = "WARN: disk; ERROR: request"; echo preg_replace_callback_array($rules, $logEntry);
Output:
[warning]: disk; [error]: request
This is a useful starting point, but production code should check errors and validate values before performing the operation. Callback patterns run in array order, so an earlier replacement can affect a later pattern. Always check for invalid patterns.
Important Behavior and Edge Cases
Callback patterns run in array order, so an earlier replacement can affect a later pattern. Always check for invalid patterns.
- Input: Use the expected PHP types and validate user-provided values.
- Result: Check the return value separately from any modifications to the input.
- Environment: For extension, database, or filesystem examples, verify availability and handle failures before accessing results.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
preg_replace_callback_array() | Apply several regular expression replacements with a separate callback for each pattern. |
| Related functions | preg_replace_callback() uses one pattern and callback; preg_replace() uses literal replacements. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Assuming preg_replace_callback_array() behaves like every similar function | Callback patterns run in array order, so an earlier replacement can affect a later pattern. Always check for invalid patterns. |
| Choosing a related operation without checking differences | preg_replace_callback() uses one pattern and callback; preg_replace() uses literal replacements. |
| Ignoring prerequisites or return values | Verify extensions and data sources, and handle unexpected results explicitly. |
Key Takeaways
preg_replace_callback_array()lets you apply several regular expression replacements with a separate callback for each pattern.- Callback patterns run in array order, so an earlier replacement can affect a later pattern. Always check for invalid patterns.
- preg_replace_callback() uses one pattern and callback; preg_replace() uses literal replacements.
- Check input, return values, and environmental requirements when adapting the examples.
preg_replace_callback_array() provides a focused PHP operation for apply several regular expression replacements with a separate callback for each pattern. Start with the simple examples, then adapt the practical example with the correct checks for your project.