0
0
PhpHow-ToBeginner · 3 min read

How to Extract Numbers from String in PHP Easily

In PHP, you can extract numbers from a string using preg_match_all with a regular expression to find digits, or use filter_var with FILTER_SANITIZE_NUMBER_INT to keep only numbers and signs. These methods help you get all or just the numeric parts from any text.
📐

Syntax

Here are two common ways to extract numbers from a string in PHP:

  • preg_match_all('/\d+/', $string, $matches); - Finds all groups of digits in the string.
  • filter_var($string, FILTER_SANITIZE_NUMBER_INT); - Removes all characters except digits, plus and minus signs.

Explanation:

  • preg_match_all searches the string for all matches of the pattern \d+ which means one or more digits.
  • filter_var cleans the string to keep only numbers and signs, useful for extracting a single number.
php
$string = "Price: 123 dollars and 45 cents.";
preg_match_all('/\d+/', $string, $matches);
// $matches[0] will contain all numbers found

$numberOnly = filter_var($string, FILTER_SANITIZE_NUMBER_INT);
// $numberOnly will be '12345'
💻

Example

This example shows how to extract all numbers from a string and print them as an array, and also how to get a single cleaned number string.

php
<?php
$string = "Order 42 costs 150 dollars and 30 cents.";

// Extract all numbers as array
preg_match_all('/\d+/', $string, $matches);
print_r($matches[0]);

// Extract numbers only as a single string
$numberOnly = filter_var($string, FILTER_SANITIZE_NUMBER_INT);
echo "\nNumbers only string: " . $numberOnly;
?>
Output
Array ( [0] => 42 [1] => 150 [2] => 30 ) Numbers only string: 421530
⚠️

Common Pitfalls

Common mistakes when extracting numbers from strings in PHP include:

  • Using preg_match instead of preg_match_all which only finds the first number.
  • Expecting filter_var to return separate numbers; it returns a single string with all digits combined.
  • Not handling decimal points or negative signs if needed.

To handle decimals, you can adjust the regex pattern to /[\d\.]+/ and for negatives /[-]?\d+/.

php
<?php
// Wrong: only first number extracted
preg_match('/\d+/', 'abc123def456', $match);
echo $match[0]; // Outputs 123 only

// Right: all numbers extracted
preg_match_all('/\d+/', 'abc123def456', $matches);
print_r($matches[0]); // Outputs [123, 456]
Output
123 Array ( [0] => 123 [1] => 456 )
📊

Quick Reference

Summary tips for extracting numbers from strings in PHP:

  • Use preg_match_all('/\d+/', $string, $matches) to get all numbers as an array.
  • Use filter_var($string, FILTER_SANITIZE_NUMBER_INT) to get a string with only digits and signs.
  • Adjust regex to include decimals or negatives if needed.
  • Remember preg_match finds only the first match.

Key Takeaways

Use preg_match_all with '/\d+/' to extract all numbers from a string as an array.
filter_var with FILTER_SANITIZE_NUMBER_INT returns a string containing only digits and signs.
preg_match finds only the first number, so use preg_match_all for multiple numbers.
Modify regex patterns to handle decimals or negative numbers if your data requires it.
Always test your extraction method with your specific string format to avoid surprises.