Search code examples
phpdata-extraction

extracting numbers from string in php


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


Solution

  • Try this :

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

    From this post Extract numbers from a string