How to extract numbers from a string in PHP?
Regular expressions can be used to extract numbers from a string, for example using the preg_match_all function to match all numbers.
$str = "Hello123World456";
preg_match_all('/\d+/', $str, $matches);
$numbers = $matches[0];
print_r($numbers);
output result:
Array
(
[0] => 123
[1] => 456
)
In the code above, the regular expression /\d+/ is used to match consecutive numbers, and then the preg_match_all function is used to save all the matching results in the $matches variable. Finally, the extracted numbers are printed out.