When dealing with string replacement, PHP provides several powerful tools, among which preg_replace_callback is a widely used function that allows us to callback the matching results using regular expressions. However, since PHP 7, a more powerful function has been introduced: preg_replace_callback_array , which not only performs similar tasks, but also brings us greater flexibility and readability.
This article will explain in depth how these two functions are used and their respective advantages through comparing examples, and explore why preg_replace_callback_array is a more elegant choice when dealing with multi-pattern matching.
preg_replace_callback receives a regular pattern array and a callback function, and processes all matches using the same callback.
Example:
$pattern = ['/foo/', '/bar/'];
$callback = function ($matches) {
return strtoupper($matches[0]);
};
$text = "foo and bar";
$result = preg_replace_callback($pattern, $callback, $text);
// result: "FOO and BAR"
The problem is that different modes use the same callback function to process logic , and for complex matches, the code will become bloated and unclear.
preg_replace_callback_array allows us to specify different callback functions for different regular modes , thus achieving greater flexibility and readability.
preg_replace_callback_array(array $patterns_and_callbacks, string $subject)
Example:
$text = "Visit http://m66.net or send mail to info@m66.net";
$result = preg_replace_callback_array([
// replaceURL
'~https?://[^\s]+~' => function ($matches) {
return '<a href="' . $matches[0] . '">' . $matches[0] . '</a>';
},
// replaceEmail
'/[\w\.-]+@[\w\.-]+\.\w+/' => function ($matches) {
return '<a href="mailto:' . $matches[0] . '">' . $matches[0] . '</a>';
},
], $text);
// result: 'Visit <a href="http://m66.net">http://m66.net</a> or send mail to <a href="mailto:info@m66.net">info@m66.net</a>'
By specifying a clear callback function for each pattern, the code logic can be clearer, and no longer need to use if/else in the same function to determine which matching pattern is.
You can provide completely different processing logic for each matching pattern, such as one for linking to HTML, and the other for sensitive word substitution, which does not interfere with each other.
Because each pattern is bound to the corresponding callback, it avoids logical confusion or mishandling that may occur when multiple patterns share a function.
preg_replace_callback_array performs replacement operations in array order, which means you can control the priority - let some replacement operations be performed before other modes.
$text = "Please visit http://m66.net,Or contact admin@m66.net,or @john Learn more。";
$result = preg_replace_callback_array([
// URL
'~https?://[^\s]+~' => function ($matches) {
return '<a href="' . $matches[0] . '">' . $matches[0] . '</a>';
},
// Email
'/[\w\.-]+@[\w\.-]+\.\w+/' => function ($matches) {
return '<a href="mailto:' . $matches[0] . '">' . $matches[0] . '</a>';
},
// @user
'/@(\w+)/' => function ($matches) {
return '<a href="http://m66.net/user/' . $matches[1] . '">@' . $matches[1] . '</a>';
},
], $text);
The result will become rich text output, suitable for forums, comment areas, chat systems, etc.
preg_replace_callback_array was introduced in PHP 7.0 and cannot be used if you run in older versions (such as PHP 5.6).
If you want to maintain support for older versions of PHP, you can only use the traditional preg_replace_callback .
preg_replace_callback_array brings us a more elegant regular replacement solution, especially when multiple modes require different processing logic. If your project is already running in PHP 7.0 or later, it is highly recommended to migrate the original multi-mode processing logic based on preg_replace_callback , which not only improves code readability, but also makes maintenance easier.
The future PHP development is no longer just "just use it", but it should be written elegantly, clear and easy to maintain.