tags:

views:

60

answers:

2

How do I fill an array like this:

array('0' => 'blabla','1' => 'blabla2')

from a string like this:

'#blabla foobar #blabla2'

using preg_match()?

+7  A: 

You should use preg_match_all() for that:

preg_match_all('/#(\S+)/', $str, $matches, PREG_PATTERN_ORDER);
$matches = $matches[1];
Greg
A: 
$string = "#wefwe dcdfg qwe #wef";
preg_match_all('/#(\w+)/', $string, $matches);
var_dump($matches);
array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(6) "#wefwe"
    [1]=>
    string(4) "#wef"
  }
  [1]=>
  array(2) {
    [0]=>
    string(5) "wefwe"
    [1]=>
    string(3) "wef"
  }
}

That is one way to do it :-)

Thorbjørn Hermansen