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: [greg] How to strip dollar signs and/or commas?

By furcat - October 26, 2012

Thank you very much. That is what I needed.

I also need a little more info, though.

When creating a search using numeric values, such as what I have shown below, how do I force the search to strip any dollar signs or commas that was placed in the field "purchase_price"?

<select name="purchase_price_min">
<option value="0">&lt;minimum&gt;</option>
<option value="0">$0</option>
<option value="500000">$500,000</option>
<option value="1000000">$1,000,000</option>
</select>
&nbsp;
<select name="purchase_price_max">
<option value="">&lt;maximum&gt;</option>
<option value="500000">$500,000</option>
<option value="1000000">$1,000,000</option>
<option value="">&gt;$1,000,000</option>
</select>

Thank you.

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

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.