Extract numbers from a string in PHP
This article demonstrates how to extract numbers from a string in PHP.
1. Using preg_replace() function
The idea is to identify all non-numeric characters in a string, and replace them with an empty string (""). In PHP, this can be done using the preg_replace() function. You can use the regex \D+ or [^0-9]+ to match one or more non-numeric characters, which can be easily modified to extract any additional characters from the string (like +, -, ., ,):
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $str = 'X:+2.15, Y:-1.50, Z:0'; var_dump(preg_replace('/\D+/', '', $str)); /* Output: string(5) "2151500" */ var_dump(preg_replace('/[^0-9-\+\.,]+/', '', $str)); /* Output: string(5) string(13) "+2.15,-1.50,0" */ ?> |
You can use the preg_match_all() function to get an array of all matches, as illustrated below. Here, \d+ matches one or more numeric characters.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php $str = 'X:+2.15, Y:-1.50, Z:0'; preg_match_all('/\d+/', $str, $matches); print_r($matches[0]); /* Array ( [0] => 2 [1] => 15 [2] => 1 [3] => 50 [4] => 0 ) */ preg_match_all('/(\+|-){0,1}\d+\.{0,1}\d*/', $str, $matches); print_r($matches[0]); /* Array ( [0] => +2.15 [1] => -1.50 [2] => 0 ) */ ?> |
2. Using filter_var() function
PHP has a built-in function called filter_var() that filters a variable with a specified filter. The FILTER_SANITIZE_NUMBER_FLOAT filter removes all characters from the string except digits, plus, and minus, and optionally eE. This filter will also remove the decimal character (.) unless FILTER_FLAG_ALLOW_FRACTION flag is specified. You may also use the FILTER_SANITIZE_NUMBER_INT which also removes all characters from the string except digits and +/-, but doesn’t accept any flag.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php $x = 'X:+2.15'; $result = filter_var($x, FILTER_SANITIZE_NUMBER_INT); var_dump($result); // string(5) "+215" $result = filter_var($x, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); var_dump($result); // string(5) "+2.15" $y = 'Y:-1.50'; $result = filter_var($y, FILTER_SANITIZE_NUMBER_INT); var_dump($result); // string(5) "-150" $result = filter_var($y, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); var_dump($result); // string(5) "-1.50" ?> |
Note that both the above solutions preserve all plus, minus, and decimal signs. For example, the string "X--5.2" returns "Y--5.2". For example,
|
1 2 3 4 5 6 7 8 9 |
<?php $str = 'X--5.2++Y'; $result = preg_replace('/[^0-9-\+\.]+/', '', $str); var_dump($result); // string(8) "--5.2++" $result = filter_var($str, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); var_dump($result); // string(8) "--5.2++" ?> |
That’s all about extracting numbers from a string in PHP.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)