How to extract Numbers From a String in PHP ? (original) (raw)

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:

Table of Content

**Using preg_replace() function

We can use the **preg_replace() function for the extraction of numbers from the string.

**Example 1:

PHP `

string=′string = 'string= 90,000,000.0098'; echo preg_replace("/[^0-9]/", '', $string); echo "\n
"; string2=′string2 = 'string2= 90,000,000.0098'; echo preg_replace("/[^0-9\.]/", '', $string2); ?>

`

Output

900000000098 90000000.0098

**Example 2: The complete code for extracting number from the string is as follows

PHP `

string=′string = 'string= 90,000,000.0098'; echo preg_replace("/[^0-9]/", '', $string); echo "\n
"; string2=′string2 = 'string2= 90,000,000.0098'; echo preg_replace("/[^0-9\.]/", '', $string2); echo "\n
"; $string3 = 'Jack has 10 red and 14 blue balls'; echo preg_replace("/[^0-9]/", '', $string3); echo "\n"; ?>

`

Output

900000000098 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 `

characters=strsplit(characters = str_split(characters=strsplit(string); numbers=arrayfilter(numbers = array_filter(numbers=arrayfilter(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 `

string=′string = 'string= 90,000,000.0098'; intnumber=filtervar(int_number = filter_var(intnumber=filtervar(string, FILTER_SANITIZE_NUMBER_INT); echo $int_number; echo "\n
"; $float_number = filter_var( $string, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); echo $float_number; echo "\n
"; $string2 = "Jack has 10 red and 14 blue balls"; intnumber2=filtervar(int_number2 = filter_var(intnumber2=filtervar(string2, FILTER_SANITIZE_NUMBER_INT); echo $int_number2; echo "\n
"; ?>

`

Output

900000000098
90000000.0098
1014

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 `

string,string, 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(numbers = extractNumbers(numbers=extractNumbers(string); // Print the extracted numbers print_r($numbers); ?>

`

Output

Array ( [0] => 123 [1] => 45.67 [2] => 890.01 )