Jump to content

PHP Question - Stripping Currency Input


sentinel

Recommended Posts

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?

Link to comment
Share on other sites

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 $.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...