Assuming the answer to a related question works,
You should be able to use the following working with Simple HTML DOM
$site = "http://siteyourgettinglinksfrom.com";
$doc = str_get_html($code);
foreach ($doc->find('a[href]') as $a) {
$href = $a->href;
if (/* $href begins with a absolute URL path */) {
$a->href = 'http://www.site.com?'.$href;
}
else{ /* $href begins with a relative path */
$a->href = 'http://www.site.com?'.$site.$href;
}
}
$code = (string) $doc;
or
Using PHP’s native DOM library:
$site = "http://siteyourgettinglinksfrom.com";
$doc = new DOMDocument();
$doc->loadHTML($code);
$xpath = new DOMXpath($doc);
foreach ($xpath->query('//a[@href]') as $a) {
$href = $a->getAttribute('href');
if (/* $href begins with a absolute URL path */) {
$a->setAttribute('href', 'http://www.site.com?'.$href);
}
else{ /* $href begins with a relative path */
$a->setAttribute('href', 'http://www.site.com?'.$site.$href);
}
}
$code = $doc->saveHTML();
Checking the $href:
you would be checking for a relative link and prepend the address of the site your pulling the content from, since most sites use relative links. (this is where a regular expression matcher would be your best friend)
for relative links you prepend the absoute path to the site which you are getting links from
'http://www.site.com?'.$site.$href
for absolute links you just append the relative link
'http://www.site.com?'.$href
Example links:
site relative: /images/picture.jpg
document relative: ../images/picture.jpg
absolute: http://somesite.com/images/picture.jpg
(Note: there is a little more work that needs done here, because if your handling "document relative" links, then you will have to know what directory you're currently in. Site relative links should be good to go, as long as you have the root folder of the site you're getting links from)