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!


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


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.

Greg Thomas

PHP Programmer - interactivetools.com

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

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:


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

Greg Thomas

PHP Programmer - interactivetools.com