如何仅在美元符号1($ 1)中替换正则表达式preg替换中的字符?

Let's say I have this preg replace:

$emailnote = preg_replace('@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', '<a href="'.SITE_DOMAIN.'index/goto.php?file_id='.$id.'&href=$1">$1</a>', $emailnote);

It's replacing text url's into clickable links, but in the format I need, because I need to process that click a certain way before redirecting them to their final destination. Anyway, You see the two dollar sign 1's ($1)? I want to replace characters for the first one but not the second one, and also I only want to replace the characters just for the text after href=, not for the whole $emailnote string/variable. i need to convert any ampersands to the html for ampersand (&), but just for the href parameter value. not the ampersand right before href= and no where else in emailnote.

For example a note might have this:

http://example.com/folder/?something&ok blah blah blah & blah

When the form is submitted with that text in text box and i go to process it. i have the database saving it exactly as is and then i email it as well and i only need it converted for the email. i need the email to display it like this:

<a href="http://example2.com/index/goto.php?file_id=123&href=http://example.com/folder/?something&amp;ok">http://example.com/folder/?something&ok</a> blah blah blah & blah

Things i tried that don't work...

Thing 1:

$emailnote = str_replace('&','&amp', preg_replace('@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', '<a href="'.SITE_DOMAIN.'index/goto.php?file_id='.$id.'&href=$1">$1</a>', $emailnote));

Thing 2:

function ampReplace($str) {
    $str = str_replace('&', '&amp;', $str);
    return $str;
}
//only 1 of these at a time, not all at once...
//$emailnote = preg_replace('@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', '<a href="'.SITE_DOMAIN.'index/goto.php?file_id='.$id.'&href='.ampReplace($1).'">$1</a>', $emailnote);
//$emailnote = preg_replace('@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', '<a href="'.SITE_DOMAIN.'index/goto.php?file_id='.$id.'&href='.ampReplace('$1').'">$1</a>', $emailnote);
$emailnote = preg_replace('@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', '<a href="'.SITE_DOMAIN.'index/goto.php?file_id='.$id.'&href='.ampReplace("$1").'">$1</a>', $emailnote);

Thanks for any help.

EDIT

don't mind url encoding whole href parameter value and not just converting the one ampersand, but how do i go about doing that?

for example the output would be like this, but like i said, i don't know how to accomplish that:

<a href="http://example2.com/index/goto.php?file_id=123&href=http%3A%2F%2Fexample.com%2Ffolder%2F%3Fsomething%26ok">http://example.com/folder/?something&ok</a> blah blah blah & blah

EDIT

The next things i tried which didn't work.

//$emailnote = preg_replace('@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', '<a href="'.SITE_DOMAIN.'index/goto.php?file_id='.$id.'&href='.urlencode('\\1').'">$1</a>', $emailnote);
//$emailnote = preg_replace('@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', '<a href="'.SITE_DOMAIN.'index/goto.php?file_id='.$id.'&href='.urlencode('\$1').'">$1</a>', $emailnote);
//$emailnote = preg_replace('@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', '<a href="'.SITE_DOMAIN.'index/goto.php?file_id='.$id.'&href='.urlencode('$1').'">$1</a>', $emailnote);
//$emailnote = preg_replace('@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', '<a href="'.SITE_DOMAIN.'index/goto.php?file_id='.$id.'&href='.urlencode("$1").'">$1</a>', $emailnote);
//$emailnote = preg_replace('@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', '<a href="'.SITE_DOMAIN.'index/goto.php?file_id='.$id.'&href='.urlencode('\\$1').'">$1</a>', $emailnote);
$emailnote = preg_replace('@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', '<a href="'.SITE_DOMAIN.'index/goto.php?file_id='.$id.'&href='.urlencode("\\$1").'">$1</a>', $emailnote);

Solution was preg_replace_callback, which i seen mentioned a few times, but didn't know how to implement it, but i figured it out.

$emailnote = preg_replace_callback("@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@",
function($matches) {
    return "<a href=\"".SITE_DOMAIN."index/goto.php?file_id=".$id."&href=".urlencode($matches[1])."\">".$matches[1]."</a>";
},
$emailnote);

The following return line might work better if ampersand gets converted to this: %26amp%3B --- it's supposed to be just %26

return "<a href=\"".SITE_DOMAIN."index/goto.php?file_id=".$_POST['id']."&href=".urlencode(str_replace('&amp;','&',$matches[1]))."\">".$matches[1]."</a>";