Format string / float for currency in PHP

1

I'm doing reports in PHP and I have a value column, except that this column can come with values with point OR commas. I need it to display the value, show it in the form of money. Example of some values as is in the Database:

0,0123
1.7481
0.4875
,14571

Example how I need it to show in the report:

R$ 0,01
R$ 1,74
R$ 0,49
R$ 0,15

I can not develop any functions that suit me in these cases, does anyone know how I can develop this function?

    
asked by anonymous 27.09.2017 / 16:59

1 answer

1

I hope this code is the solution

 $num = array("11.111.090,00" , "111.090,00", "090,00" , "0.4875");

 foreach ($num as $value) {
     if (strpos($value, "0") == 0){
         $value = str_replace(',', '.', $value);
         $value = number_format($value, 2, ',', '.');
         echo "R$ " . $value . " reais<br>";
     }else{
         $value = str_replace('.', '', $value);
         $value = str_replace(',', '.', $value);
         $value = number_format($value, 2, ',', '.');
         echo "R$ " . $value . " reais<br>";
     }
 }
    
04.10.2017 / 00:02