When working with text data in PHP, you might need to extract numerical values from a string. This is a common task in data processing, such as parsing user input or extracting data from strings containing mixed content. In this tutorial you will learn how to extract numbers from a string in PHP with different approaches.
Using Regular Expressions with preg_match_all()
Regular expressions are a powerful way to match and extract specific patterns from strings. You can use the preg_match_all()
function with /\d+.\d+|\d+/
regex pattern in PHP to find and extract all numbers from a given string. Here is an example:
$inputString = 'The price of the product is $99.99, and the discount is 20%.';
preg_match_all('/\d+\.\d+|\d+/', $inputString, $matches);
print_r($matches[0]);
Array
(
[0] => 99.99
[1] => 20
)
In this example, the regular expression /\d+.\d+|\d+/
is used to match and extract numbers from the $inputString. It captures both decimal numbers (with or without a decimal point) and whole numbers.
Using preg_replace() to Remove Non-Numeric Characters
Another approach to extract numbers from a string is to use preg_replace()
to remove all non-numeric characters, leaving only the numbers.
$inputString = 'The price of the product is $99.99, and the discount is 20%.';
$integer = preg_replace('/[^0-9]+/', '', $inputString);
$double = preg_replace('/[^0-9.]+/', '', $inputString);
echo $integer . PHP_EOL;
echo $double . PHP_EOL;
999920
99.9920.
In this example, the preg_replace('/[^0-9.]+/', '', $inputString)
statement replaces all non-numeric characters (including decimal points) with an empty string, effectively leaving only the numbers.
Using filter_var() to Extract Numbers from URL String
PHP’s filter_var function can be utilized to filter and extract numeric values from a string. This approach is particularly useful if you’re dealing with numbers that follow a specific format. For instance, you can extract numbers following the format of a URL:
$string = "Visit https://example.com/2023 for more information.";
$filtered = filter_var($string, FILTER_SANITIZE_NUMBER_INT);
echo $filtered;
2023
In this example, FILTER_SANITIZE_NUMBER_INT
is used to filter out integer numbers from the string. The result will be “2023.”
Wrap the PHP Number Extraction Code in a Custom Function
You can also create custom functions to extract numbers from a string. This approach is particularly useful if you have specific requirements for extracting numeric values.
function extractNumbers($string) {
$matches = [];
preg_match_all("/\d+(\.\d+)?/", $string, $matches);
return $matches[0];
}
$string = "The product IDs are P123, P456, and P789. Their prices are $12.99, $24.95, and $30.00.";
$numbers = extractNumbers($string);
print_r($numbers);
Array
(
[0] => 123
[1] => 456
[2] => 789
[3] => 12.99
[4] => 24.95
[5] => 30.00
)
In this example, the extractNumbers()
function encapsulates the regular expression matching process. It returns an array of numeric values found in the input string.