When developing multilingual applications, we often need to insert dynamic variables into the template. PHP provides many ways to implement this replacement function, where preg_replace_callback_array is a very powerful tool to replace tags dynamically in complex text, especially when text needs to support multiple languages and include different variables.
preg_replace_callback_array is a regular expression replacement function in PHP. It allows you to dynamically handle replacement operations with a callback function. Unlike the normal preg_replace , preg_replace_callback_array can handle multiple different regular patterns and define corresponding callback functions for each pattern, which makes it very useful when dealing with complex text replacements.
preg_replace_callback_array(array $patterns_and_callbacks, string $subject): string
$patterns_and_callbacks : This is an associative array, the keys of the array are regular expression patterns, and the values are callback functions.
$subject : The target string to be replaced.
Suppose we are developing an application that supports multilingual and needs to dynamically replace variable tags in different languages. Assuming that the text template contains two variables: {username} and {email} , we can use preg_replace_callback_array to dynamically replace the values of these variables.
// Simulate user information
$user = [
'username' => 'JohnDoe',
'email' => 'john@example.com',
];
// Define language templates
$template = "welcome, {username}! Your email is: {email}";
// Mapping of regular mode and callback function
$patterns_and_callbacks = [
'/\{username\}/' => function($matches) use ($user) {
return $user['username'];
},
'/\{email\}/' => function($matches) use ($user) {
return $user['email'];
},
];
// usepreg_replace_callback_arrayMake a replacement
$finalText = preg_replace_callback_array($patterns_and_callbacks, $template);
echo $finalText;
In the above code, we first define an array $user containing user information. Then, a $template string containing multilingual text is defined, which contains the two variable labels {username} and {email} . Next, we map the regular expression pattern and callback function through an associative array $patterns_and_callbacks , and finally use the preg_replace_callback_array function to perform the replacement operation.
welcome, JohnDoe! Your email is: john@example.com
In practical applications, we often need to dynamically load different language templates according to the user's language selection. With preg_replace_callback_array , we can dynamically replace variables in templates with multilingual arrays.
// Simulate user language
$lang = 'zh';
// Multilingual templates
$translations = [
'en' => "Welcome, {username}! Your email is: {email}",
'zh' => "welcome, {username}! Your email is: {email}",
];
// User Information
$user = [
'username' => 'JohnDoe',
'email' => 'john@example.com',
];
// Select the template for the current language
$template = $translations[$lang];
// Mapping of regular mode and callback function
$patterns_and_callbacks = [
'/\{username\}/' => function($matches) use ($user) {
return $user['username'];
},
'/\{email\}/' => function($matches) use ($user) {
return $user['email'];
},
];
// usepreg_replace_callback_arrayMake a replacement
$finalText = preg_replace_callback_array($patterns_and_callbacks, $template);
echo $finalText;
In this example, we first load the corresponding template text based on the user's language selection. Then, variable replacement is performed through regular and callback functions as before.
welcome, JohnDoe! Your email is: john@example.com
Sometimes, we may need to dynamically replace the URL's domain name in the template, especially when the domain name in the environment changes. For example, if you use a different URL during development and need to switch to the production environment later, we can use preg_replace_callback_array to dynamically replace the domain name in the URL.
// Simulate user information
$user = [
'username' => 'JohnDoe',
'email' => 'john@example.com',
];
// The template contains URL
$template = "Please visit our website: https://www.old-domain.com/profile/{username}";
// Mapping of regular mode and callback function
$patterns_and_callbacks = [
'/https?:\/\/(www\.[a-zA-Z0-9\-\.]+)\//' => function($matches) {
return str_replace($matches[1], 'm66.net', $matches[0]);
},
'/\{username\}/' => function($matches) use ($user) {
return $user['username'];
},
];
// usepreg_replace_callback_arrayMake a replacement
$finalText = preg_replace_callback_array($patterns_and_callbacks, $template);
echo $finalText;
Please visit our website: https://www.m66.net/profile/JohnDoe
In this example, we use a regular expression to match the URL and replace the domain name with m66.net . This operation allows us to dynamically adjust URLs in different environments.
preg_replace_callback_array is a very flexible and powerful tool, especially suitable for scenarios where variable tags need to be dynamically replaced in multilingual text. By combining regular expressions and callback functions, you can easily deal with complex text replacement requirements, supporting dynamic loading and variable replacement in multiple languages, making the code more flexible and maintainable.