Answers for "php get only numeric from string"

PHP
6

php keep only numbers in string

$str = preg_replace('/[^0-9.]+/', '', $str);
Posted by: Guest on February-25-2020
2

php get only numbers from string

$str = 'In My Cart : 11 items';
$int = (int) filter_var($str, FILTER_SANITIZE_NUMBER_INT);
Posted by: Guest on November-24-2020
0

php get number from string

function get_numerics ($str) {
    preg_match_all('/\d+/', $str, $matches);
    return $matches[0];
}

// this function will return an array of number

$str = "3 dogs were running!";
echo (get_numerics($str)[0]); 

// output 3
Posted by: Guest on August-24-2021

Code answers related to "php get only numeric from string"

Browse Popular Code Answers by Language