zeeks
zeeks

Reputation: 794

get all characters after all @ character

I am making a tag system, but I am facing some problem when a user tags more than 1 user.

This is my code:

$data = "test test test test @brian_2 test test @john Spelling @test_3 test test test test";

if (($pos = strpos($data, "@")) !== FALSE) {
    $whatIWant = substr($data, $pos+1);
    $whatIWant = substr($whatIWant, 0, strpos($whatIWant, ' '));
}

echo $whatIWant;

But that only echos the first tagged user, in this case brian_2.

I tried to make a while to get all users, but it does not work.

Any idea?

Thanks

Upvotes: 1

Views: 60

Answers (1)

dynamic
dynamic

Reputation: 48131

Better using regex for this:

preg_match_all("/@[A-Za-z0-9_]+/",$text, $matches);
print_r($matches);

Live: http://ideone.com/ILYkYf

Output:

Array
(
    [0] => Array
        (
            [0] => @brian_2
            [1] => @john
            [2] => @test_3
        )

)

Upvotes: 4

Related Questions