Tags
PHP
Asked 6 years ago
2 Oct 2018
Views 1128
sarah

sarah posted

how to convert money format to integer value in php

suppose 32,440 . it should shows plain format like 32440

how to convert money format to integer value in php ?
dilip

dilip
answered Apr 25 '23 00:00

To convert a money format string to an integer value in PHP, you can use the combination of preg_replace and intval functions. preg_replace function is used to remove any non-numeric characters from the string, and then intval function converts the resulting string to an integer. Here's an example code snippet:



<?php
$money = "$1,234.56"; // Example money format string
$int = intval(preg_replace('/[^0-9]/', '', $money));
echo $int; // Output: 1234
?>

In this example, we first define the money format string $money. We then use the preg_replace function with regular expression '/[^0-9]/' to remove any non-numeric characters from the string. The resulting string 123456 is then passed to the intval function, which converts it to the integer 1234.

Note that this example assumes that the money format string uses a period . as the decimal separator. If your money format string uses a different decimal separator, such as a comma ,, you may need to replace it with a period . before using intval
Post Answer