How to extract Numbers From a String in PHP ?
Last Updated :
23 Jul, 2025
Extracting numbers from a string involves identifying and isolating numerical values embedded within a text. This process can be done using programming techniques, such as regular expressions, to filter out and retrieve only the digits from the string, ignoring all other characters.
Here we have some common approaches:
Using preg_replace() function
We can use the preg_replace() function for the extraction of numbers from the string.
- /[^0-9]/ pattern is used for finding number as integer in the string (Refer to Example 1)
- /[^0-9\.]/ pattern is used for finding number as double in the string (Refer to Example 2)
Example 1:
PHP
<?php
$string = '$ 90,000,000.0098';
echo preg_replace("/[^0-9]/", '', $string);
echo "\n<br/>";
$string2 = '$ 90,000,000.0098';
echo preg_replace("/[^0-9\.]/", '', $string2);
?>
Output900000000098
90000000.0098
Example 2: The complete code for extracting number from the string is as follows
PHP
<?php
$string = '$ 90,000,000.0098';
echo preg_replace("/[^0-9]/", '', $string);
echo "\n<br/>";
$string2 = '$ 90,000,000.0098';
echo preg_replace("/[^0-9\.]/", '', $string2);
echo "\n<br/>";
$string3 = 'Jack has 10 red and 14 blue balls';
echo preg_replace("/[^0-9]/", '', $string3);
echo "\n";
?>
Output900000000098
90000000.0098
1014
Using str_split() and ctype_digit()
Using str_split() and ctype_digit() in PHP, you can extract numbers from a string by splitting it into characters, filtering out the digits, and then joining them back together.
Example:
PHP
<?php
$string = "The price is 100 dollars and 50 cents.";
$characters = str_split($string);
$numbers = array_filter($characters, 'ctype_digit');
echo implode('', $numbers) . PHP_EOL; // Output: 10050
?>
Using filter_var() and Regular Expressions
The filter_var() function with the FILTER_SANITIZE_NUMBER_INT or FILTER_SANITIZE_NUMBER_FLOAT filters can be used to extract numbers from a string. This approach is useful when you need to sanitize a string and extract an integer or float.
Example : Extracting Integer and Float Numbers Using filter_var()
Here is the complete code for extracting an integer and a float number from a string using filter_var():
PHP
<?php
$string = '$ 90,000,000.0098';
$int_number = filter_var($string, FILTER_SANITIZE_NUMBER_INT);
echo $int_number;
echo "\n<br/>";
$float_number = filter_var(
$string,
FILTER_SANITIZE_NUMBER_FLOAT,
FILTER_FLAG_ALLOW_FRACTION
);
echo $float_number;
echo "\n<br/>";
$string2 = "Jack has 10 red and 14 blue balls";
$int_number2 = filter_var($string2, FILTER_SANITIZE_NUMBER_INT);
echo $int_number2;
echo "\n<br/>";
?>
Output900000000098
<br/>90000000.0098
<br/>1014
<br/>
Using preg_match_all()
The preg_match_all() function is used to perform a global regular expression match, which allows us to find all instances of numbers within a string. This method is flexible and can be tailored to extract integers, floats, or any other specific numerical patterns.
Example
PHP
<?php
// Function to extract all numbers (both integers and floats) from a string
function extractNumbers($string) {
// Define the regular expression pattern for numbers (both integers and floats)
$pattern = '/\d+(\.\d+)?/';
// Use preg_match_all to find all matches
preg_match_all($pattern, $string, $matches);
// Return the array of matched numbers
return $matches[0];
}
// Test string containing numbers
$string = "The prices are 123, 45.67 and 890.01 dollars.";
// Extract numbers from the string
$numbers = extractNumbers($string);
// Print the extracted numbers
print_r($numbers);
?>
OutputArray
(
[0] => 123
[1] => 45.67
[2] => 890.01
)