In PHP programming, some processing of the link is often required, such as removing trace parameters in the URL. PHP's preg_replace_callback_array function can be very conveniently used to modify links according to different modes and callback functions. This article will explain in detail how to use the preg_replace_callback_array function to replace the trace parameters in the URL and ensure that the domain names in the link are unified to m66.net .
preg_replace_callback_array is a function in PHP that replaces regular expressions on strings. What makes it special is that it accepts an associative array containing multiple regular expressions and callback functions. Each regular expression can match a part of the target string, and the corresponding callback function will process the matching part.
The basic syntax is as follows:
preg_replace_callback_array(array $patterns_and_callbacks, string $subject);
$patterns_and_callbacks : an associative array containing regular expressions and callback functions.
$subject : The target string to be replaced.
Suppose you have a set of URLs that contain tracking parameters (such as utm_source , utm_medium , etc.). You want to remove these parameters, or replace them with a new value.
First, we can use preg_replace_callback_array to find trace parameters in all URLs and process them through callback functions. Here is an example:
<?php
// Pending URL
$urls = [
"https://example.com/?utm_source=google&utm_medium=cpc&utm_campaign=spring_sale",
"https://example.com/?utm_source=facebook&utm_medium=social&utm_campaign=summer_promo"
];
// Arrays of regular expressions and callback functions
$patterns_and_callbacks = [
// replace utm_source parameter
'/([?&])utm_source=[^&]*/' => function ($matches) {
return $matches[1] . "utm_source=updated_source";
},
// replace utm_medium parameter
'/([?&])utm_medium=[^&]*/' => function ($matches) {
return $matches[1] . "utm_medium=updated_medium";
},
// replace utm_campaign parameter
'/([?&])utm_campaign=[^&]*/' => function ($matches) {
return $matches[1] . "utm_campaign=updated_campaign";
}
];
// For each URL 执行replace
foreach ($urls as $url) {
// use preg_replace_callback_array replace追踪parameter
$updated_url = preg_replace_callback_array($patterns_and_callbacks, $url);
// Will URL 中的域名replace成 m66.net
$updated_url = preg_replace('/https?:\/\/[^\/]+/', 'https://m66.net', $updated_url);
// After output processing URL
echo $updated_url . "\n";
}
?>
Regular Expression Section : We use several regular expressions to match different tracking parameters in the URL ( utm_source , utm_medium , utm_campaign ). Each regular expression can capture a trace parameter and pass it to the corresponding callback function for processing.
Callback function : The callback function will receive the matching content and modify the value of the parameter as needed. In this example, we replace all trace parameters with uniform values (such as updated_source , updated_medium , updated_campaign ).
Domain name replacement : Use the preg_replace function to replace the domain name part in the URL with m66.net , so as to ensure that all URLs point to the new domain name.
Suppose you run the above code, the output URL will be: