I assume you are using file_get_contents() to grab the entire source of the page you are linking to and simply outputting the result.
What you would do is use preg_match_all() on the result before outputting it to replace anything that looks like a link with your own link.
Edit 1
I agree with the other answer given than regex can be a minefield with stuff like this. parsing the DOM can be very challenging too. An interesting alternative would be to output the source as is but inject some JavaScript/jQuery which would be a far easier way to interact with the DOM. The script could simply modify the href attributes of any anchor tags it finds.
I'll have a bash at writing it later on unless someone beats me to it!
Edit 2
Here you go. I've tested this on a selection of links and it seems to be good to go. Obviously is subject to your visitor having javascript enabled, but it seems to me like the simplest "one size fits all" solution.
<script>
$(function(){
$("a").each(
function(){
oldlink = $(this).attr("href");
insertpoint = oldlink.indexOf("//") + 2;
if (insertpoint = 2) {
insertpoint = 0;
}
newlink = oldlink.substr(0,insertpoint) + "www.mywebsite.com/myscript.php?link=" + encodeURIComponent(oldlink.substr(insertpoint));
$(this).attr("href",newlink);
}
);
});
</script>
Don't forget to include jQuery for this to work!
Download it here https://jquery.com/download/
or just include it like this
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>