1$str = 'In My Cart : 11 items';
2$int = (int) filter_var($str, FILTER_SANITIZE_NUMBER_INT);
1phpCopy<?php
2$string = 'Sarah has 4 dolls and 6 bunnies.';
3$outputString = preg_replace('/[^0-9]/', '', $string);
4echo("The extracted numbers are: $outputString \n");
5?>
6
1phpCopy<?php
2$string = 'Sarah has 4 dolls and 6 bunnies.';
3$int = (int) filter_var($string, FILTER_SANITIZE_NUMBER_INT);
4echo("The extracted numbers are: $int \n");
5?>
6
1phpCopy<?php
2$string = 'Sarah has 4 dolls and 6 bunnies.';
3preg_match_all('!\d+!', $string, $matches);
4print_r($matches);
5?>
6
1function get_numerics ($str) {
2 preg_match_all('/\d+/', $str, $matches);
3 return $matches[0];
4}
5
6// this function will return an array of number
7
8$str = "3 dogs were running!";
9echo (get_numerics($str)[0]);
10
11// output 3