-1

I'm wondering how to extract numbers from a string for example

$string = "1.8 to 250"

What i want to get is,

$a = 1.8
$b = 250

Thanks for any help provided

fizjin
  • 141
  • 1
  • 9
  • This is for decimal numbers http://stackoverflow.com/questions/14687284/extract-decimal-or-integer-from-a-string-in-php – Manwal Sep 22 '15 at 07:32

2 Answers2

1

Try this :

$str = '1.8 to 250';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);

From this post Extract numbers from a string

Community
  • 1
  • 1
Fky
  • 2,133
  • 1
  • 15
  • 23
0

Try this:

   $str = '1.8 to 250';
   $string = explode(" ",$str);
   //print_r($string);
  for($i = 0;$i < count($string);$i++){
     if(is_numeric($string[$i])){
        print "\n $string[$i]";
     }  
  }
JammuPapa
  • 138
  • 1
  • 1
  • 10