2

I am working in my php function. My requirement is to convert :

1,234

into

1234

I know it can be achieved by str_replace but is any inbuilt PHP function exists for this or not.

2 Answers2

2

You could use http://php.net/number_format as such:

number_format(1.234,3,'','');

and this will output:

1234

In addition, you could see this post PHP: get number of decimal digits to get a good idea on how to automate the second parameter of the above function.

Community
  • 1
  • 1
SiGm4
  • 284
  • 1
  • 2
  • 10
0

str_replace is not bad to do this. BTW, there is a multiple alternatives. You could use one of the from below:

1 preg_replace

$a = '1,234';
$a = preg_replace('/[.,]/', '', $a);

Regular expression for simple character substitution is overkill.

2 strstr

$a = '1,234';
$b = strtr($a, array('.' => '', ',' => ''));

You'd be better off using strstr. Above both example will help you.

AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57