MyBB Community Forums

Full Version: Trying to make a good Amazon replacement link
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I don't know if this is possible with MyCode, but imagine how useful this would be - to automatically convert any Amazon link in a post to a better-looking link with affiliate info added.

This is a typical amazon link:
http://www.amazon.com/Baby-Einstein-Take-Along-Tunes/dp/B000YDDF6O/ref=pd_sim_t_1

When someone pastes the link in a post, mMyBB automatically converts it to this:
<a href="http://www.amazon.com/Baby-Einstein-Take-Along-Tunes/dp/B000YDDF6O/ref=pd_sim_t_1">http://www.amazon.com/Baby-Einstein-Take...pd_sim_t_1</a>
Example:
http://www.amazon.com/Baby-Einstein-Take...pd_sim_t_1

Is it possible to automatically convert it to this:
<a href="http://www.amazon.com/dp/B000YDDF6O&tag=your_Associates_ID">Baby-Einstein-Take-Along-Tunes</a>
Example:
Baby-Einstein-Take-Along-Tunes

OK, now onto the problem. First, I am simply trying to successfully detect an Amazon.com link and add in the referral code.

Here's what I have so far :

Expression:
<a(.*?)href="http(?:s)?://(?:www\.)?amazon\.com/(.*?)"(.*?)>(.*?)</a>

Replacement:
<a target="_blank" href="http://www.amazon.com/$2&tag=affiliate-20">$4</a>

This works on most links. The problem I'm looking at is that there are so many ways an Amazon link can look, including some with no descriptors.

Now I'm trying to combine this with another bit of regex to find a valid ASIN and generate a better link. This is to try to fix the few messed-up Amazon link types I have seen that do not work with the above script.

Getting closer...

OK. I have a new version. This one seems to find the ASIN code in some messier Amazon links and builds a new link. the problem is that unlike the one I posted above, it convert a single link, and trim out the other two... trying to figure that problem out.

OK. my code works as a "global" regex replace, but not otherwise. As far as I can tell the forum does not work that way, or I just don't know how to add that feature into my Expression. Any regex experts care to chime in?
It looks like we'll have to write javascript to run at the end of every page to change all of the amazon links based on my latest regex... myCodes don't seem to have that ability.
yes you need JS. That is how VigLink and others do it
care to share the code? And do you have it fully working?
Put this in your footer"

<script>//is 1/2/13
var nodes = document.getElementsByTagName("a");
for (x=0;x<nodes.length;x++) {
	if(nodes[x].href.toLowerCase().indexOf("amazon.com") > -1 && (nodes[x].href.toLowerCase().indexOf("/dp/") > -1 || nodes[x].href.toLowerCase().indexOf("/gp/product/") > -1)){
		var strHref = nodes[x].href;
		var ASIN;
		if(strHref.toLowerCase().indexOf("/dp/") > -1)
			ASIN = strHref.substr(strHref.toLowerCase().indexOf("/dp/") + 4, 10);
		if(strHref.toLowerCase().indexOf("/gp/product/") > -1)
			ASIN = strHref.substr(strHref.toLowerCase().indexOf("/gp/product/") + 12, 10);
		nodes[x].href = "http://amazon.com/dp/" + ASIN + "/&tag=yourtag-20";
	}
}
nodes = null;
</script>
i don't understand hic
I'm reviving this decade-old thread because I have a similar problem, though I approached it differently, given that it's 2023.

First, here is my Custom MyCode for detecting Amazon links:

Regular Expression:
https:\/\/www\.amazon\.com\/(.{1,})(\/[a-zA-Z0-9]{10}\/)([^\s\]\!\.]{0,})


Replacement:
$1$2$3tag=myaffiliate-20


It detects an Amazon URL and does the substitution, adding in the correct tag, flawlessly.

The problem, however, is that it will only do one substitution per post. If a user posts three different Amazon URLs, it'll only change the last one.

I have similar Twitter Embed MyCode, and that'll run the substitution on as many Twitter URLs as someone puts in their post, so I can't figure out why this Amazon one doesn't work the same way.

Any ideas would be greatly appreciated!