I want to extract the digits from a string that contains numbers and letters like:
"In My Cart : 11 items"
I want to extract the number 11
.
If you just want to filter everything other than the numbers out, the easiest is to use filter_var:
$str = 'In My Cart : 11 items';
$int = (int) filter_var($str, FILTER_SANITIZE_NUMBER_INT);
$str = 'In My Cart : 11 12 items';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);
preg_replace('/[^0-9]/', '', $string);
This should do better job!...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With