如何从 PHP 中的链接中删除跟踪代码
How to delete tracking code from links in PHP
您好,我在 WordPress 中有一个表单,用户可以在其中向产品提交 link,但通常 link 带有不必要的包袱,例如跟踪代码。我想在 WordPress 中创建一个过滤器并清理 link,这样它们就只包含一个有效的 link。如果可能的话,我想确认 link 仍然有效或保证 link 仍然有效的方法。
我想在 link 中删除的主要内容是 utm_source
及其内容、utm_medium
及其内容等。除了干净的工作之外的所有内容 link.
例如,像这样的 link:
https://www.serenaandlily.com/variationproduct?dwvar_m10055_size=Twin&dwvar_m10055_color=Chambray&pid=m10055&pdp=true&source=detail&utm_source=affiliate&utm_medium=affiliate&utm_campaign=pjdatafeed&publisherId=20648&clickId=2669312134#fo_c=745&fo_k=c0ebaf8359ca7853df8343e535533280&fo_s=pepperjam
会变成这样:
https://www.serenaandlily.com/variationproduct?dwvar_m10055_size=Twin&dwvar_m10055_color=Chambray&pid=m10055
如果有人能指引我正确的方向,我将不胜感激。
谢谢!
您可以在 PHP 本身中执行此操作。有一个名为 parse_url()
(https://secure.php.net/manual/en/function.parse-url.php) which can give you all the URI params as array. After parsing, you can filter the parameters, remove the unwanted. Finally, use http_build_query()
(https://secure.php.net/manual/en/function.http-build-query.php) 的函数可以构建到 return 的字符串 URI :)
你可以用 explode
, parse_str
and http_build_query
做你想做的事。此代码使用不需要的参数数组来决定从查询字符串中删除什么:
$unwanted_params = array('utm_source', 'utm_medium', 'utm_campaign', 'clickId', 'publisherId', 'source', 'pdp', 'details', 'fo_k', 'fo_s');
$url = 'https://www.serenaandlily.com/variationproduct?dwvar_m10055_size=Twin&dwvar_m10055_color=Chambray&pid=m10055&pdp=true&source=detail&utm_source=affiliate&utm_medium=affiliate&utm_campaign=pjdatafeed&publisherId=20648&clickId=2669312134#fo_c=745&fo_k=c0ebaf8359ca7853df8343e535533280&fo_s=pepperjam';
list($path, $query_string) = explode('?', $url, 2);
// parse the query string
parse_str($query_string, $params);
// delete unwanted parameters
foreach ($unwanted_params as $p) unset($params[$p]);
// rebuild the query
$query_string = http_build_query($params);
// reassemble the URL
$url = $path . '?' . $query_string;
echo $url;
输出:
https://www.serenaandlily.com/variationproduct?dwvar_m10055_size=Twin&dwvar_m10055_color=Chambray&pid=m10055
您好,我在 WordPress 中有一个表单,用户可以在其中向产品提交 link,但通常 link 带有不必要的包袱,例如跟踪代码。我想在 WordPress 中创建一个过滤器并清理 link,这样它们就只包含一个有效的 link。如果可能的话,我想确认 link 仍然有效或保证 link 仍然有效的方法。
我想在 link 中删除的主要内容是 utm_source
及其内容、utm_medium
及其内容等。除了干净的工作之外的所有内容 link.
例如,像这样的 link:
https://www.serenaandlily.com/variationproduct?dwvar_m10055_size=Twin&dwvar_m10055_color=Chambray&pid=m10055&pdp=true&source=detail&utm_source=affiliate&utm_medium=affiliate&utm_campaign=pjdatafeed&publisherId=20648&clickId=2669312134#fo_c=745&fo_k=c0ebaf8359ca7853df8343e535533280&fo_s=pepperjam
会变成这样:
https://www.serenaandlily.com/variationproduct?dwvar_m10055_size=Twin&dwvar_m10055_color=Chambray&pid=m10055
如果有人能指引我正确的方向,我将不胜感激。
谢谢!
您可以在 PHP 本身中执行此操作。有一个名为 parse_url()
(https://secure.php.net/manual/en/function.parse-url.php) which can give you all the URI params as array. After parsing, you can filter the parameters, remove the unwanted. Finally, use http_build_query()
(https://secure.php.net/manual/en/function.http-build-query.php) 的函数可以构建到 return 的字符串 URI :)
你可以用 explode
, parse_str
and http_build_query
做你想做的事。此代码使用不需要的参数数组来决定从查询字符串中删除什么:
$unwanted_params = array('utm_source', 'utm_medium', 'utm_campaign', 'clickId', 'publisherId', 'source', 'pdp', 'details', 'fo_k', 'fo_s');
$url = 'https://www.serenaandlily.com/variationproduct?dwvar_m10055_size=Twin&dwvar_m10055_color=Chambray&pid=m10055&pdp=true&source=detail&utm_source=affiliate&utm_medium=affiliate&utm_campaign=pjdatafeed&publisherId=20648&clickId=2669312134#fo_c=745&fo_k=c0ebaf8359ca7853df8343e535533280&fo_s=pepperjam';
list($path, $query_string) = explode('?', $url, 2);
// parse the query string
parse_str($query_string, $params);
// delete unwanted parameters
foreach ($unwanted_params as $p) unset($params[$p]);
// rebuild the query
$query_string = http_build_query($params);
// reassemble the URL
$url = $path . '?' . $query_string;
echo $url;
输出:
https://www.serenaandlily.com/variationproduct?dwvar_m10055_size=Twin&dwvar_m10055_color=Chambray&pid=m10055