dtrj21373 2018-06-12 17:41
浏览 98
已采纳

如何仅在美元符号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);
  • 写回答

1条回答 默认 最新

  • douqian5553 2018-06-12 19:47
    关注

    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>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备