There is a function called preg_match_all
The first parameter takes a regular expression. The following example matches at least one digit, followed by any number of digits. This will match the numbers.
The second parameter is the string itself, the topic from which you want to extract
The third is an array in which all matched elements will sit. Thus, the first element will be 123, the second - 589, etc.
preg_match_all("/[0-9]+/", $string, $matches);
source share