1

现在我有代码:

$msgs = preg_replace('/(?<=^|\s)@([a-z0-9_]+)/i', '$1', $msg);

让我们这么说

$msg = "@Admin Hello my friends";

代码在上面有效,但我只需要获取标记的名称!我只需要得到“管理员”,所有被标记的人。我怎么做?

4

1 回答 1

1

你可以做:

$msgs = preg_replace('/(?<=^|\s)@(\w+).*$/', '$1', $msg);

或者

if (preg_match('/(?<=^|\s)@(\w+)/', $msg, $match)) {
    $msgs = $match[1];
}

如果您可以@在一行中有多个,请使用preg_match_all

preg_match_all('/(?<=^|\s)@(\w+)/', $msg, $match)
于 2014-01-09T08:20:58.743 回答