Skip to content

Instantly share code, notes, and snippets.

@jonathonbyrdziak
Created July 22, 2011 17:03
Show Gist options
  • Save jonathonbyrdziak/1099866 to your computer and use it in GitHub Desktop.
Save jonathonbyrdziak/1099866 to your computer and use it in GitHub Desktop.
Working with currency and string to integers in php
<?php
/**
* Function is responsible for stripping anything out of a string value that is meant to
* be an currency amount or other float. This function then converts the numbers left
* into a float value and formatting it appropriately.
*
* @param string $string | Can be anything but mostly for $23.23
* @return float $string | returns 23.23 as a valid integer that can be used in math.
*/
function _mint( $string )
{
$string = str_replace(array('$','&#036;'), '', $string);
$string = trim(preg_replace("([^0-9.])", "", $string));
$string = number_format(floatval($string), 2, '.', ',');
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment