3 回答
TA贡献2019条经验 获得超9个赞
尝试这个
<?php
$url = '/resutls?offer=1&gclid=1&utm_source=21213';
$url = strrpos( $url, '&gclid')?substr($url, 0, strrpos( $url, '&gclid')):$url;
$url = strrpos( $url, '&utm_source')?substr($url, 0, strrpos( $url, '&utm_source')):$url;
$url = strrpos( $url, '?utm_campaign')?substr($url, 0, strrpos( $url, '?utm_campaign')):$url;
$url = strrpos( $url, '?utm_medium')?substr($url, 0, strrpos( $url, '?utm_medium')):$url;
$url = strrpos( $url, '?token')?substr($url, 0, strrpos( $url, '?token')):$url;
$url = strrpos( $url, '&fbclid')?substr($url, 0, strrpos( $url, '&fbclid')):$url;
$url = strrpos( $url, '&utm_campaign')?substr($url, 0, strrpos( $url, '&utm_campaign')):$url;
$url = strrpos( $url, '&utm_medium')?substr($url, 0, strrpos( $url, '&utm_medium')):$url;
$url = strrpos( $url, '&token')?substr($url, 0, strrpos( $url, '&token')):$url;
echo $url;
?>
TA贡献1906条经验 获得超10个赞
使用parse_url()和parse_str()获取您想要的任何关键字。
$parsed = parse_url('/resutls?offer=1&gclid=CjwKCAjw1f_pBRAEEiwApp0JK');
parse_str($parsed['query'], $output);
echo($output['offer']); // should print 1
您也可以使用它来重建您的 URL:
echo $parsed['path'] . '?offer=' . $output['offer']; // prints /resutls?offer=1
TA贡献1828条经验 获得超6个赞
我已经将“如果找到后删除”包装到一个函数中。这首先检查字符串是否存在,然后如果存在,它将执行该substr()部分...
function trimIfSet ( string $base, string $pattern ) {
if (( $pos = strrpos($base, $pattern)) !== false ){
$base = substr($base, 0, $pos);
}
return $base;
}
$url = '/resutls?offer=1&gclid=CjwKCAjw1f_pBRAEEiwApp0JK';
$url = trimIfSet( $url, '&gclid');
$url = trimIfSet( $url, '&utm_source');
echo $url;
- 3 回答
- 0 关注
- 182 浏览
添加回答
举报