sentinel Posted July 4, 2006 Share Posted July 4, 2006 I guess I really should just go find a programming forum to post this in, but I'm too damn lazy. I'm doing a form with PHP and I have a feild which contains currency - including the '$' signs, and potentially commas. I'm currently storing it in the MySQL DB as a string, but it would be much more useful if I could strip out those extra characters and store it as a double or float. Anyone know how to do this in PHP? Quote Link to comment Share on other sites More sharing options...
sharpnyourteeth Posted July 4, 2006 Share Posted July 4, 2006 use the ereg_replace function. here's an example: <?php $money = "$3,301.98"; $formatted = ereg_replace('$|,', "", $money); echo $formatted; //prints out: 3301.98 ?> ereg_replace replaces the regex (first parameter) with the second paramter from the string in the third parameter. so this is saying replace any $'s or commas with nothing. Make sure you escape the $. Quote Link to comment Share on other sites More sharing options...
sentinel Posted July 4, 2006 Author Share Posted July 4, 2006 awesome! That looks perfect, Thanks!! :) Quote Link to comment Share on other sites More sharing options...
l0gic Posted July 4, 2006 Share Posted July 4, 2006 An alternate, possibly more effecient way to accomplish this would be using str_replace. This eliminates the need for a full-blown regex for such a menial task. You can create an array containing the character set you wish to strip from the input. http://us2.php.net/str_replace Quote Link to comment Share on other sites More sharing options...
sentinel Posted July 5, 2006 Author Share Posted July 5, 2006 Thanks guys! I used str_replace() and it worked flawlessly! :) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.