0

我试图从用 PHP 运行的网站的 URL 中检测会员 ID。URL 示例是

我已将会员 ID 更改为真实的。

https://flipkart.com/product-name/product-id?marketplace=FLIPKART&affid=honor&ppn=something
https://flipkart.com/product-name/product-id?marketplace=FLIPKART&affid=apple&ppn=something
https://flipkart.com/product-name/product-id?marketplace=FLIPKART&affid=sony&ppn=something

我想用affid=sthachilo

这些 URL 将保存在数据库中。所以我不能用$_GET["affid"]

当显示 URL 时,我只想替换会员 ID。或者我可以用它来替换会员 ID 并将其保存在数据库中。但我不知道如何检测该affid=部分并替换那里的任何内容。

我尝试了以下代码,但它似乎不起作用

    $url = "https://flipkart.com/product-name/product-id?marketplace=FLIPKART&affid=sony&ppn=something";
    $newurl = preg_replace('/&affid=(\d+)/','&affid=sthachilo',$url);
    echo $newurl;

这是一个真实的 URL 示例,其中包含我的会员 ID。

https://www.flipkart.com/complan-kesar-badam/p/itmew2cdj5dmthjk?marketplace=FLIPKART&iid=e6c16ab0-c15d-49c2-843e-cf4a5104c53b.MDMETGN5YZZ5MRYG.SEARCH&ppt=browse&lid=LSTMDMETGN5YZZ5MRYGTBFFJO&srno=b_1_1&pid=MDMETGN5YZZ5MRYG&affid=sthachilo&ssid=7bwqga35j40000001588133282410&ppn=browse

至少我想删除之后的所有内容itmew2cdj5dmthjk 并在最后添加会员 ID。例如

https://www.flipkart.com/complan-kesar-badam/p/itmew2cdj5dmthjk?affid=sthachilo

提前致谢

4

2 回答 2

1

您可以使用parse_url获取 url 部分,而不是使用parse_str获取 url 查询

$url = "https://flipkart.com/product-name/product-id?marketplace=FLIPKART&affid=sony&ppn=something";
// parse url
$parse = parse_url($url);
// parse the query
parse_str($parse['query'], $query);
// replace the affid with your desired value
$query['affid'] = 'samsung';
// rebuild the query
$parse['query'] = http_build_query($query);
// rebuild the final url
$final_url = $parse['scheme'].'://'.$parse['host'].$parse['path'].'?'.$parse['query'];
echo $final_url
// output : https://flipkart.com/product-name/product-id?marketplace=FLIPKART&affid=samsung&ppn=something
于 2020-04-29T05:02:10.857 回答
0

要将给定的会员 ID 替换为android

$url = "https://flipkart.com/product-name/product-id?marketplace=FLIPKART&affid=apple&ppn=something";

$newurl = preg_replace('/&affid=(.+)?&/','&affid=android&',$url);

可能不是最好的方法,因为它假设affid被包围,&但如果你知道情况总是如此,那么这将起作用。

于 2020-04-29T05:05:44.467 回答