How to strip dollar signs and/or commas?

5 posts by 2 authors in: Forums > CMS Builder
Last Post: November 2, 2012   (RSS)

By furcat - October 25, 2012

Are there any functions for stripping off commas or dollar signs from numbers? I need to use numbers entered by my customer in calculations, and don't want the commas.

But, for display purposes, the dollar sign and commas makes reading the numbers on the web page a lot easier.

Any help or advice is welcome!

Thanks.

Re: [furcat] How to strip dollar signs and/or commas?

By gregThomas - October 26, 2012

Hi,

If you want to strip the dollar sign and comma from a number you could use these functions together:

$value = preg_replace('/[\$,]/', '', $value);
$value = floatval($value);


The preg_replace function will strip out the dollar sign and any commas. The floatval function will convert the number from a string to a floating value, ensuring it will behave like a number if you want to use it for addition, multiplication, etc.

To convert the number back to a currency format I would use this:

echo '$ '.number_format($number, 2, '.', ',');

The number_format function will take a number and convert it into string format it. In this case it makes the number have two decimal places and adds a comma for every 3 digits.

Thanks!
Greg Thomas







PHP Programmer - interactivetools.com

Re: [furcat] How to strip dollar signs and/or commas?

By gregThomas - October 29, 2012

CMS Builder doesn't come with a feature that allows you to do a search replace on a MySQL string while carrying out a search. Your best option would be to ensure that the data that is entered into CMS Builder is all in the same format to allow easy searching. If this isn't an option there are a couple of things you could try:

1)Writing a script that would go through and replace all of the values in the table and strip out the dollar signs and commas. You could do that using something like this:

list($mainContents, $test_1MetaData) = getRecords(array(
'tableName' => 'tableName',
'loadUploads' => true,
'allowSearch' => false,
));

foreach ($mainContents as $key => $row){
$tempValue = $row['currencyField'];
$tempValue = preg_replace('/[\$,]/', '', $tempValue);
$tempValue = floatval($tempValue);
mysql_update('test', $row['num'], null,array('currencyField' => $tempValue));
}


Ensure you have backed up your database before using something like this, if it doesn't behave as expected it could corrupt your data.

The second option is to write your own custom MySQL statement that uses the replace function that is built in. You can find out more about it here:

http://dev.mysql.com/doc/refman/5.0/en/replace.html

Once you have created your search string you can use the mysql_fetch function to execute it.

Thanks!
Greg Thomas







PHP Programmer - interactivetools.com

Re: [greg] How to strip dollar signs and/or commas?

By furcat - November 2, 2012

Thanks guys. I think I'm going to restrict data entry to numbers only, and then put the correct format for dollar amounts when I display the data.