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
1$str = 'In My Cart : 11 items';
2$int = (int) filter_var($str, FILTER_SANITIZE_NUMBER_INT);
1$num = "3.14";
2$int = (int)$num;//string to int
3$float = (float)$num;//string to float
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
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