get only numbers from string php code example
Example 1: php keep only numbers in string
$str = preg_replace('/[^0-9.]+/', '', $str);
Example 2: php get only numbers from string
$str = 'In My Cart : 11 items';
$int = (int) filter_var($str, FILTER_SANITIZE_NUMBER_INT);
Example 3: Extract Numbers From a String in PHP
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");
?>