B-219 Sec-55 Noida, India
+918010221733

How to extract numbers from a string in PHP

Do you want to extract numbers from a string using PHP? Use the below code for it
<?php
$str="I have 12 dogs and 20 cats";
preg_match_all('!\d+!', $str, $matches);
print_r($matches);
?>
This code will give output in an array format like given below. Array ( [0] => Array ( [0] => 12 [1] => 20 ) )
(Visited 31 times, 1 visits today)