How to extract numbers from string in PHP ? - GeeksforGeeks

21 May, 2021
In this article, we will extract numbers from strings using PHP. There are various built-in methods to extract numbers from strings, some of them are discussed below.
Methods:
Using preg_match_all() Function.
Using  filter_var() Function.
Using  preg_replace() function.
Method 1: Using preg_match_all() Function.
Note:  preg_match() function is used to extract numbers from a string. Usually, the search starts from the beginning of the subject string. The optional parameter offset is used to specify the position from where to start the search.
Syntax:
int preg_match( $pattern, $input, $matches, $flags, $offset )
Return value: It returns true if a pattern exists, otherwise false.
Example 1: The following example extracts Integer Numbers using the preg_match_all() function.
PHP
<?php// PHP program to illustrate// preg_match function// Declare a variable and initialize it$geeks = 'Welcome 2 Geeks 4 Geeks.';  // Use preg_match_all() function to check matchpreg_match_all('!\d+!', $geeks, $matches);  // print output of functionprint_r($matches);  ?>
Array
(
    [0] => Array
        (
            [0] => 2
            [1] => 4
        )

)
Example 2: The following example extracts decimal numbers using the preg_match_all() function.
PHP
<?php// Declare a variable and initialize it$geeks = 'Welcome 2 Geeks 4.8 Geeks.';  // Use preg_match_all() function to check matchpreg_match_all('!\d+\.*\d*!', $geeks, $matches);  // Display matches resultprint_r($matches);  ?>
Array
(
    [0] => Array
        (
            [0] => 2
            [1] => 4.8
        )

)
Method 2: Using filter_var() function.
Note: The filter_var() function filters a variable with the specified filter. This function is used to both validate and sanitize the data.
Syntax:
filter_var(var, filtername, options)
Return Value: It returns the filtered data on success, or FALSE on failure.
Example:
PHP
<?php// PHP program to illustrate filter_var Function// Declare a variable and initialize it$geeks = 'Welcome 2 Geeks 4 Geeks.';  // Filter the Numbers from String$int_var = (int)filter_var($geeks, FILTER_SANITIZE_NUMBER_INT);  // print output of functionecho ("The numbers are: $int_var \n");  ?>
The numbers are: 24 
Method 3: Using  preg_replace() function.
Note: The preg_replace() function is an inbuilt function in PHP that is used to perform a regular expression for search and replace the content.
Syntax:
preg_replace( $pattern, $replacement, $subject, $limit, $count )
Return Value: This function returns an array if the subject parameter is an array, or a string otherwise.
Example:
PHP
<?php      // PHP program to illustrate   // preg_replace function     // Declare a variable and initialize it   $geeks = 'Welcome 2 Geeks 4 Geeks.';    // Filter the Numbers from String  $int_var = preg_replace('/[^0-9]/', '', $geeks);       // print output of function   echo("The numbers are: $int_var \n");     ?>
The numbers are: 24 
PHP-function
PHP-Questions
PHP
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to fetch data from localserver database and display on HTML table using PHP ?
Different ways for passing data to view in Laravel
Create a drop-down list that options fetched from a MySQL database in PHP
How to create admin login page using PHP?
How to generate PDF file using PHP ?
PHP Cookies
PHP | shell_exec() vs exec() Function
PHP str_replace() Function
How to pass form variables from one page to other page in PHP ?
PHP | Ternary Operator