-2

Given a string:

$string = "Hello, 1992 world! Today's 2016! That's 24 years!";

How can I parse the string to return an array containing all the numbers inside?

Expected result:

{1992,2016,24}
Hard Spocker
  • 765
  • 11
  • 32

2 Answers2

2

Using the preg_match_all() you can achieve what you want-

$str = "Hello, 1992 world! Today's 2016! That's 24 years!";
preg_match_all('!\d+!', $str, $matches);
print_r($matches);

Online Example

Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
1
$str = "Hello, 1992 world! Today's 2016! That's 24 years!";
preg_match_all("!\d+!", $str, $matches);
print_r($matches);
Martin
  • 628
  • 1
  • 9
  • 28