Try this:
preg_match_all('/#(\w+)/', $str, $matches);
Example:
<?php
$str = '#test #test2 #123 qwe asd #rere#dada';
preg_match_all('/#(\w+)/', $str, $matches);
var_export($matches);
Output:
array (
0 =>
array (
0 => '#test',
1 => '#test2',
2 => '#123',
3 => '#rere',
4 => '#dada',
),
1 =>
array (
0 => 'test',
1 => 'test2',
2 => '123',
3 => 'rere',
4 => 'dada',
),
)
I think learning RegEx will help you solve these problems.