php tag system, get just username from @

858 views Asked by At

Right now I have the code:

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

Let's say that

$msg = "@Admin Hello my friends";

The code works above but I need to get just the tagged name! I need to get just "Admin", all the persons who have been tagged. How do I do that?

1

There are 1 answers

8
Toto On

You can do:

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

or

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

If you can have more than one @ in a single line, use preg_match_all:

preg_match_all('/(?<=^|\s)@(\w+)/', $msg, $match)