0

I have this:

$text = $_POST['text'];

When I echo $text I get this:

ggg #hhh #ddd ggg hhhrr ggg #ttt 

When I do this:

$val = preg_match_all("/#\w+/", $text, $matches);
print_r($matches);

I get

Array ( [0] => Array ( [0] => #hhh [1] => #ddd [2] => #ttt ) )

But I want output like this:

Array ( [0] => #hhh [1] => #ddd [2] => #ttt ) 

thanks

Laf
  • 7,965
  • 4
  • 37
  • 52
Mahmoud Samy
  • 183
  • 1
  • 2
  • 10

2 Answers2

1

Another approach is to use named groups.

$val = preg_match_all("/(?P<myLabel>#\w+)/", $text, $matches);
print_r($matches['myLabel']);
HamZa
  • 14,671
  • 11
  • 54
  • 75
mirk
  • 442
  • 4
  • 13
0

Take the first (zeroth) item from the array by changing this:

print_r($matches);

To this:

print_r($matches[0]);
nickb
  • 59,313
  • 13
  • 108
  • 143