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&ok">http://example.com/folder/?something&ok</a> blah blah blah & blah
Things i tried that don't work...
Thing 1:
$emailnote = str_replace('&','&', 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('&', '&', $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);