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}
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}
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);
$str = "Hello, 1992 world! Today's 2016! That's 24 years!";
preg_match_all("!\d+!", $str, $matches);
print_r($matches);