最開始的 $content 表示外部網頁的內容
<?php
$content = 'fdsfsfs <a href=URL1>TEXT1</a> <a href=URL2>TEXT2</a> <a href=URL3>TEXT3</a> xzcasfas';
$matches = array();
$aryPreset = array('URLa','URLb','URLc');
if(preg_match_all("/<a [^>]+>([^<>]+)<\/a>/i", $content, $matches) == 3) {
$rlink1 = (rand(1,10) <= 3) ? ('<a href="'.$aryPreset[rand(0,2)].'">'.$matches[1][0].'</a>') : '';
$rlink2 = (rand(1,10) <= 3) ? ('<a href="'.$aryPreset[rand(0,2)].'">'.$matches[1][1].'</a>') : '';
$rlink3 = (rand(1,10) <= 3) ? ('<a href="'.$aryPreset[rand(0,2)].'">'.$matches[1][2].'</a>') : '';
//var_dump($matches);
$arySearch = $matches[0];
$aryReplace = array(
$matches[0][0].$rlink1,
$matches[0][1].$rlink2,
$matches[0][2].$rlink3
);
$content = str_replace($arySearch, $aryReplace, $content);
echo $content;
}
>