1

I am seeing a currency symbol  when I store my data into a MySQL table, I have tried trim(), but still the  has not been removed.

JavaScript:

var numZAR=new Intl.NumberFormat("en-ZAR",{    
    style:"currency",
    currency:"ZAR",
});

var total_raw = numZAR.format(Number(final_item_total).toFixed(2));

var total_string=total_raw.toString();

var total_show = total_string;
$('#final_total_amt').text(total_show);

$('#me').val(total_show); //displaying on front end textbox

PHP code before the MySQL statement:

$final = $_POST["me"];

$final = trim($final,"ZAR ");

This is what shows in the table: Â 2,212.00

Please help! enter image description here

benny
  • 11
  • 3
  • first see why `Â` is saving into db column. change column `collation to` `float` to prevent it. also use `parseFloat()` in your jQuery code too – Alive to die - Anant Nov 18 '19 at 09:26
  • Hi, I think there was a missing parentheses in the first line of code, which I tried to fix. Please review all your code very carefully that there are no other mistakes. For a solution, I would be inclined to store a [hidden field](https://stackoverflow.com/a/1000811/673991) that just has the number with no currency symbol. And then in PHP store that in the MySQL table, no trim necessary. – Bob Stein Nov 18 '19 at 10:22
  • Hey, 'final_item_total' is a float variable, changing _column collation to float_ is giving me 0 value in my colum – benny Nov 21 '19 at 09:09

3 Answers3

1

trim() is used for removing specific characters at the beginning and end of the string. whilst ltrim() is used for removing at only the beginning, and finally rtrim() for removing at only the end.

You can lookup its prototype here.

Because it seems like you are using it incorrectly.

Let $chars be the string containing individual characters that you want to remove from the beginning and end of the string. Then you would have:

$chars = 'Â';
$final = $_POST["me"];
$final = trim($final, $chars);

$final would have the value of

2,212.00

Because 'Â' would have been removed from the beginning.

Now as for where 'Â' comes from, its most probably caused by the Intl.NumberFormat converter. If all your backend needs is to store the values (without any currency signs) - then I don't see why just sending the values as plain numbers from JavaScript won't do job.

So just let your JavaScript be:

var total_raw = Number(final_item_total).toFixed(2);

var total_string=total_raw.toString();

var total_show = total_string;
$('#final_total_amt').text(total_show);

$('#me').val(total_show); //displaying on front end textbox 

That would just send a floating value to the server in the format: 2342.43 - then you wouldn't need to trim any "unwanted values".

  • This code works but the reason why I added Intl.NumberFormat converter because the result should be currency format e.g: $2,442.00 or $24,420.00 or $244,200.00 or $2,442,000.00 – benny Nov 21 '19 at 08:39
  • @benny then you can do that in the backend using PHP `number_format()`. I think it is best that you do the formatting on the backend because looks like the frontend was giving you problems anyways (hence your question). – Kagiso Marvin Molekwa Nov 21 '19 at 13:33
0

You should see where that  comes from, but if you really cannot find it and fix it, as a backup you could use regex to get what you want. A simple example:

$str = "Â 2,212.00";
preg_match('/[0-9,.]+/', $str, $final); 
echo $final[0];
Martin Dimitrov
  • 1,304
  • 1
  • 11
  • 25
0

if that string have fix special character position then you can use this one

$data = explode('Â', 'Â 2,212.00'); echo $data[1];

Trushar Narodia
  • 3,511
  • 2
  • 11
  • 17