Hello @ Okugbe ,
 You need few lines of code, and use regexp: 
  $string = "<p>Please <a href='http://example.com'>click here</a> to go to <a href='http://example.com'>the site</a></p>"; 
$newurl = "http://myotherexample.com"; 
$pattern = "/(?<=href=(\"|'))[^\"']+(?=(\"|'))/"; 
$newstring = preg_replace($pattern,$newurl,$string); 
Second way you can also use jquery to bind any link click. This way you can do your link eval on the fly. This jsfiddle is a rough run through of what i think you're trying to accomplish. The alerts are just for your benefit and should be removed.
$("a").click(function() {
    addAffiliate(this);
});
myCode = "?pp=708a77db476d737e54b8bf4663fc79b346d696d2";
myAmazonCode = "?tag=shihac-20"
    function addAffiliate(link) {
        alert("enterting script: " + link.href);
        if ((link.href).indexOf("gog.com") > -1 && (link.href).indexOf(myCode) < 0) {
                link.href = link.href + myCode;
        }else if((link.href).indexOf("amazon.com") > -1 && (link.href).indexOf(myAmazonCode) < 0){
                link.href = link.href + myAmazonCode;   
        }
            alert(link.href);
        return true;
    }
Here you can see my demo jsfiddle working code:http://jsfiddle.net/du47b/23/
Hope you understand the code and this help you!!
Thank You!!