PHP: How to get integer or decimal from a string?

Suppose, I have a string with text and number and I only want the number. I don’t want the characters and text of the string.

Here is the way out:-

In this example, you will get only integer. It will omit all characters and text from the string.


$string = 'Nrs 89,994,874.0098';
echo preg_replace("/[^0-9]/", '', $string);
// output: 899948740098

This example prints decimal numbers removing all the characters from the string.


$string = 'Nrs 89,994,874.0098';
echo preg_replace("/[^0-9\.]/", '', $string);
// output: 89994874.0098

Hope this helps. Thanks.