当有人提到他们在一起时,如何不在@username 中给出 link?

How to not give the link in @username when someone mention and they're together?

如果有人 post @usernameone,它会给 link。但问题是当他们 post 时它仍然给出 link 像这样的 @usernameone@usernametwo。我不知道如何修改此代码以使其工作。

function mention($text) {
$patterns = array('@(http?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '/@([A-Za-z0-9_]{1,15})/');
$replace = array('<a href=""></a>', '<a href="../../profile/">@</a>');
$result = preg_replace($patterns, $replace, $text);
return $result;
}

调用函数

<?php  echo mention($rowcom->description);?>

结果

预期结果

这段代码没有问题,只需要在$replace数组的第二个元素末尾添加一个space即可:

function mention($text) {
    $patterns = array('@(http?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '/@([A-Za-z0-9_]{1,15})/');
    $replace = array('<a href=""></a>', '<a href="../../profile/">@</a> ');
    $result = preg_replace($patterns, $replace, $text);

    return $result;
}

echo mention('@usernameone bla bla') . '<br>';
echo mention('@usernameone@usernametwo bla bla');

输出

@usernameone 啦啦啦

@usernameone @usernametwo bla bla

更新

如果您想排除 "stuck" 个用户名,您的正则表达式将变得相当复杂:

$patterns = array('@(http?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '/(^|[\s\.,;\?\!])@(?![A-Za-z0-9_]+@)([A-Za-z0-9_@]+)/');
$replace = array('<a href=""></a>', '<a href="../../profile/">@</a> ');