在@字符之后得到所有字符

时间:2014-11-29 12:30:39

标签: php

我正在制作标签系统,但当用户标记多个用户时,我遇到了一些问题。

这是我的代码:

$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;

但这只是回应第一个被标记的用户,在这种情况下为brian_2

我尝试了一段时间来获取所有用户,但它不起作用。

有什么想法吗?

由于

1 个答案:

答案 0 :(得分:4)

最好使用正则表达式:

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

直播:http://ideone.com/ILYkYf

输出:

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

)