In PHP, the preg_replace_callback_array function is a powerful tool that allows you to specify different callback functions for multiple regular expression patterns. This function associates the pattern with the callback function through an associative array and replaces it in sequence. However, how is the execution order of the callback function determined?
The preg_replace_callback_array function accepts two parameters: the first parameter is an associative array, the key of the array is the regular expression pattern, and the value is the callback function. The second parameter is the input string. Functions will pattern-by-pattern matching and replacement of input strings in the order of pattern defined in the array.
<?php
$patterns = [
'/hello/' => function ($matches) {
return "Hi";
},
'/world/' => function ($matches) {
return "Earth";
}
];
$string = "hello world! hello world!";
$result = preg_replace_callback_array($patterns, $string);
echo $result; // Output:Hi Earth! Hi Earth!
?>
In this example, the input string "hello world! hello world!" will be matched first to hello and then replaced with Hi ; then it will be matched to world and replaced with Earth . The execution order of the callback function is determined according to the order of the pattern definition in the array.
The execution order of the callback function in preg_replace_callback_array is strictly determined by the following factors:
The order of patterns in arrays : The order of execution of callback functions is determined by the order of keys (i.e. regular expression patterns) of the passed pattern array. PHP will start with the first pattern of the array and execute the callback function one by one in order.
Pattern matching priority : If multiple regular expression patterns can match the same position (for example, two patterns match the same part in a string), PHP will select the first matching pattern according to the order of the pattern array to execute the callback function. Therefore, the order of patterns is crucial.
Impact of global matching : If a pattern matches a part of the string and is replaced, the content of the new string may affect the matching of subsequent patterns. Therefore, the replaced string content will affect the execution of subsequent callback functions.
<?php
$patterns = [
'/o/' => function ($matches) {
return "O";
},
'/world/' => function ($matches) {
return "Universe";
}
];
$string = "hello world!";
$result = preg_replace_callback_array($patterns, $string);
echo $result; // Output:hellO Universe!
?>
In this example, although the /world/ pattern matches world , since the /o/ pattern is first matched and replaced with o in the string as O , the final output is hellO Universe! .
The order in which the callback function is executed becomes particularly important when multiple patterns match the same position or substring. If you want to make sure that a particular callback function is executed first, you should put the relevant regular expression pattern in front of the array.
For example, suppose you have a string containing different links and HTML tags, and you want to replace the link first and then the HTML tags. At this point, the pattern of link replacement should be ahead of the pattern of HTML tag replacement.
<?php
$patterns = [
'/https?:\/\/\S+/' => function ($matches) {
return "<a href='{$matches[0]}'>Link</a>";
},
'/<b>.*?<\/b>/' => function ($matches) {
return "<strong>{$matches[0]}</strong>";
}
];
$string = "Visit https://m66.net for more <b>information</b>!";
$result = preg_replace_callback_array($patterns, $string);
echo $result; // Output:Visit <a href='https://m66.net'>Link</a> for more <strong><b>information</b></strong>!
?>
In this example, /https?:\/\/\S+/ mode will replace the link first, and /<b>.*?<\/b>/ mode will replace the <b> tag. The final output result is that both the link and the tag are replaced correctly.
The preg_replace_callback_array function allows you to define multiple regular expression patterns and their callback functions through an associative array. The order of execution of the callback function is determined by the order of patterns in the pattern array. When multiple patterns match the same part, the preferred matching pattern will first execute the corresponding callback function. Therefore, when using preg_replace_callback_array , make sure the order of the pattern arrays meets your needs.