I'm retrieving values from MySQL numeric field and want to format with a comma separator for 1000s like this: 21,000
This code below seems to work for display - how do I strip out the commas again before updating and inserting into MySQL DB?
<input type="text" name="Price id="Price value="<?php echo number_format($row_['Price'])); ?>" size="10" />
PHP number_format and insert to DB
Moderator: General Moderators
-
energylevel
- Forum Newbie
- Posts: 4
- Joined: Mon Nov 08, 2004 5:11 am
Re: PHP number_format and insert to DB
Use str_replace() to replace the comma with an empty string. Or, better, use preg_replace() to strip out everything except digits and periods.
These could be transcription errors, but in the the HTML/PHP you posted, the name and id attributes of the input tag are missing closing quotes and there is an extra parenthesis after number_format().
Code: Select all
str_replace(',', '', '5,432.10'); // Returns "5432.10"
str_replace(',', '', ' $5,432.10 '); // Returns " $5432.10 "
preg_replace('/[^\d.]+/', '', ' $5,432.10 '); // Returns "5432.10"