9951 explained code solutions for 126 technologies


php-regexHow to get only numbers from a string using regex in PHP?


Using regex in PHP to get only numbers from a string is a simple task. The following example code block uses the preg_match_all function to extract all numbers from a string:

$string = 'This is a string with some numbers: 1, 2, 3, 4, 5';

preg_match_all('/\d+/', $string, $matches);

print_r($matches);

The output of the above code will be:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
        )

)

Code explanation

  1. $string = 'This is a string with some numbers: 1, 2, 3, 4, 5'; - This is the string from which the numbers will be extracted.

  2. preg_match_all('/\d+/', $string, $matches); - This is the preg_match_all function which is used to extract all numbers from the string. The \d+ is the regex pattern which matches all numbers.

  3. print_r($matches); - This is used to print the extracted numbers.

Helpful links

Edit this code on GitHub