I've got this working for .co.uk
You can easily add .ca, .de or any two level .tld like .co.uk to this by adding:
$associate_id_ and then the .tld with the . replaced by _
$associate_id_co_uk is the UK one.
And of course add the bit in the preg_match to work with this.
Code for the file is thus:
PHP Code:
<?php
function amazon_links($text) {
$associate_id_co_uk = 'buro9-21'; // or leave empty
$associate_id_com = 'buro9-20'; // or leave empty
$replace_targt_urls = true; // true or false
$replace_src = array();
$replace_str = array();
// check for [URL="amazon-link"]some text[/URL]
if(preg_match_all("/\[url\=(?:\")?(http:\/\/(?:[A-z0-9\.]+)?amazon\.(co\.uk|com)\/(?:.+))(?:\")?](.+)\[\/url\]/iU", $text, $out)) {
for($i=0;$i<count($out[1]);$i++) {
$associate_id = ${'associate_id_'.str_replace('.','_',$out[2][$i])};
$out[4][$i] = $out[1][$i];
if($associate_id && !empty($associate_id)) {
// Look for an asin number
if(preg_match("/\/[A-Z0-9]{10}(\/)?/", $out[1][$i], $asin) && strpos($out[1][$i], '/review') === false) {
if(substr($asin[0], -1) != '/') { $asin[0] .= '/'; }
$out[1][$i] = 'http://www.amazon.'.$out[2][$i].'/exec/obidos/ASIN'.$asin[0].'ref=nosim/'.$associate_id;
} else {
$target_url = $out[1][$i];
if($replace_targt_urls) {
$target_url = preg_replace('/ref\=([a-z0-9\-]{3,})\-([0-9]{2})/iU', 'ref='.$associate_id, $target_url);
$target_url = preg_replace('/tag\=([a-z0-9\-]{3,})\-([0-9]{2})/iU', 'tag='.$associate_id, $target_url);
}
$out[1][$i] = 'http://www.amazon.'.$out[2][$i].'/exec/obidos/redirect?link_code=ur2&camp=1789&tag='.$associate_id.'&creative=9325&path='.urlencode($target_url);
}
$replace_src[] = '[URL="'.$out[4][$i].'"]';
$replace_str[] = '[URL="'.$out[1][$i].'"]';
$replace_src[] = '[URL='.$out[4][$i].']';
$replace_str[] = '[URL='.$out[1][$i].']';
$replace_src[] = '[url="'.$out[4][$i].'"]';
$replace_str[] = '[url="'.$out[1][$i].'"]';
$replace_src[] = '[url='.$out[4][$i].']';
$replace_str[] = '[url='.$out[1][$i].']';
}
}
} unset($out);
// check for [URL]amazon-link[/URL]
if(preg_match_all("/\[url\](http:\/\/(?:[A-z0-9\.]+)?amazon\.(co\.uk|com)\/(?:.+))\[\/url\]/iU", $text, $out)) {
for($i=0;$i<count($out[1]);$i++) {
$associate_id = ${'associate_id_'.str_replace('.','_',$out[2][$i])};
$out[4][$i] = $out[1][$i];
if($associate_id && !empty($associate_id)) {
// Look for an asin number
if(preg_match("/\/[A-Z0-9]{10}(\/)?/", $out[1][$i], $asin) && strpos($out[1][$i], '/review') === false) {
if(substr($asin[0], -1) != '/') { $asin[0] .= '/'; }
$out[1][$i] = 'http://www.amazon.'.$out[2][$i].'/exec/obidos/ASIN'.$asin[0].'ref=nosim/'.$associate_id;
} else {
$target_url = $out[1][$i];
if($replace_targt_urls) {
$target_url = preg_replace('/ref\=([a-z0-9\-]{3,})\-([0-9]{2})/iU', 'ref='.$associate_id, $target_url);
$target_url = preg_replace('/tag\=([a-z0-9\-]{3,})\-([0-9]{2})/iU', 'tag='.$associate_id, $target_url);
}
$out[1][$i] = 'http://www.amazon.'.$out[2][$i].'/exec/obidos/redirect?link_code=ur2&camp=1789&tag='.$associate_id.'&creative=9325&path='.urlencode($target_url);
}
$displayed_link = $out[4][$i];
if(strlen($displayed_link) > 55) {
$displayed_link = substr($displayed_link, 0, 36).'...'.substr($displayed_link, -14);
}
$replace_src[] = '[URL]'.$out[4][$i].'[/URL]';
$replace_str[] = '[URL="'.$out[1][$i].'"]'.$displayed_link."[/URL]";
$replace_src[] = '[url]'.$out[4][$i].'[/url]';
$replace_str[] = '[url="'.$out[1][$i].'"]'.$displayed_link."[/url]";
}
}
} unset($out);
// replace the message
if(isset($replace_src[0])) {
$text = str_replace($replace_src, $replace_str, $text);
} unset($replace_src, $replace_str);
return $text;
}
?>
Thanks for the great hack.