I'm trying to create a script that can find and identify an Amazon Affiliate ID from the URL.
I know the Affiliate ID appears after the parameter "&tag=" but it's location in the URL is not always consistent. Sometimes it's in the middle:
http://www.amazon.com/gp/goldbox?&tag=adamcarolla09-20&camp=217705&creative=406565&linkCode=ur1&adid=1S1WPEJM5M1Y1RRW2875&
Sometimes it's at the end:
http://www.amazon.com/?_encoding=UTF8&camp=1789&creative=9325&linkCode=ur2&tag=schlockmercenary
How can I write a script that goes through the URL and returns just the tag, regardless of it's position in the string?
You can use PHP parse str:
<?php
// ...
parse_str($url, $output);
echo $output['tag'];
?>
Do this:
$tag = strstr($url, "tag=");
$tag = substr($tag, 4, strlen($tag) -4 -strlen(strstr($tag, "&")));
$tag = urldecode($tag);