PHP Mantenha apenas números em string
$str = preg_replace('/[^0-9.]+/', '', $str);
Bad Batfish
$str = preg_replace('/[^0-9.]+/', '', $str);
phpCopy<?php
$string = 'Sarah has 4 dolls and 6 bunnies.';
$outputString = preg_replace('/[^0-9]/', '', $string);
echo("The extracted numbers are: $outputString \n");
?>
$str = 'In My Cart : 11 items';
$int = (int) filter_var($str, FILTER_SANITIZE_NUMBER_INT);
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
phpCopy<?php
$string = 'Sarah has 4 dolls and 6 bunnies.';
$int = (int) filter_var($string, FILTER_SANITIZE_NUMBER_INT);
echo("The extracted numbers are: $int \n");
?>
phpCopy<?php
$string = 'Sarah has 4 dolls and 6 bunnies.';
preg_match_all('!\d+!', $string, $matches);
print_r($matches);
?>