简体   繁体   English

从 URL 中检测和替换会员 ID

[英]detect and replace affiliate ID from URLs

I was trying to detect affiliate ID from URLs for a website running in PHP.我试图从在 PHP 中运行的网站的 URL 中检测会员 ID。 the URL examples are URL 示例是

I have changed the affiliate ID into real ones.我已将会员 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

I want to replace it with affid=sthachilo我想用affid=sthachilo替换它

These URLs will be saved in database.这些 URL 将保存在数据库中。 So I cant use $_GET["affid"]所以我不能使用$_GET["affid"]

when the URL is shown, I just want to replace the affiliate ID.当显示 URL 时,我只想替换会员 ID。 or I can use it to replace the affiliate ID and save it in the database.或者我可以用它来替换会员 ID 并将其保存在数据库中。 But I dont know how to detect the affid= section and replace whatever is there.但我不知道如何检测affid=部分并替换那里的任何内容。

I tried the following code, but it doesnt seems to be working我尝试了以下代码,但它似乎不起作用

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

Here is a real URL example with my affiliate ID in it.这是一个真实的 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

atleast I want to remove everything after itmew2cdj5dmthjk and add affiliate ID at the end.至少我想删除itmew2cdj5dmthjk之后的所有内容并在最后添加会员 ID。 for example例如

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

Thanks in advance提前致谢

you can use parse_url to get the url parts and than use parse_str to get the url query您可以使用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

To replace the given affiliate ID with android :android替换给定的附属 ID:

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

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

Probably not the best way to do it since it assumes affid is surrounded by & but if you know that will always be the case then this will work.可能不是最好的方法,因为它假设affid&包围,但如果你知道情况总是如此,那么这将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM