danny55
danny55

Reputation:

Regular expression get info from string

How do I fill an array like this:

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

from a string like this:

'#blabla foobar #blabla2'

using preg_match()?

Upvotes: 1

Views: 160

Answers (2)

Thorbjørn Hermansen
Thorbjørn Hermansen

Reputation: 3552

$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 :-)

Upvotes: 0

Greg
Greg

Reputation: 321844

You should use preg_match_all() for that:

preg_match_all('/#(\S+)/', $str, $matches, PREG_PATTERN_ORDER);
$matches = $matches[1];

Upvotes: 7

Related Questions