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.
Related posts:
- Random number, string generation in PHP
- PHP: Simple and easy way to format URL string
- PHP: Parse Unparse String Array
- PHP: Generating Multiple Random String
- jQuery: How to replace string, div content and image src?
- Javascript: How to get current URL without Query string?
- Regular Expression check, Validation in PHP
- Convert string to datetime and datetime to string in C#
- jQuery: Grey out background and preview image as popup
- PHP : Read Write Xml with SimpleXML
