Currency formatting in PHP [duplicate]

-3

Greetings to all I am working on a system in PHP, and would like to know if there is a function that makes PHP recognize or use the currency that is set in windows!

    
asked by anonymous 04.12.2017 / 11:21

1 answer

0

When your browser requests a web page, it sends a language acceptance header ACCEPT_LANGUAGE which tells you which languages you can accept the content in and in what order.

Examples of strings returned by the header HTTP_ACCEPT_LANGUAGE

  • es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3
  • en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4

Mine on this machine is:

  • pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4

That is to say, I prefer the Brazilian Portuguese, if it has not it can be Portuguese "general" (weight 0.8). In the absence of these, I can accept US English (q = 0.6), and finally, "General English" (weight 0.4).

The parameter q (quality factor), indicates the preference order of the user.

Given this string, we can have PHP use the currency based on this information.

/*idioma do navegador do Usuário (o primeiro da lista que tem a mais alta preferencia 
e tem formatação próxima a necessária para uso em setlocale).
entre as aspas simples pode colocar uma default (exemplo: pt-BR ou en-US)
*/
$http_lang = isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]) ? substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,5) : '';

//formata para valor válido em setlocale
//substitui o "-" (tracinho) por "_" (underline)
$http_lang = str_replace("-","_",$http_lang);

//Define informações locais
setlocale(LC_ALL, $http_lang);

//retorna dados baseados na localidade corrente definida por setlocale().
$locale_info = localeconv();

//Simbolo da moeda local
$simbolo = $locale_info['currency_symbol'];
//caractere decimal
$decimal_point = $locale_info['decimal_point'];
//Separador de Milhares 
$thousands = $locale_info['thousands_sep'];

Example usage:

$valor = 12345678900;

echo $simbolo.number_format($valor,2,$decimal_point,$thousands);

View result on ideone

  

Most browsers have settings that allow you to check or change language preference settings.

    
05.12.2017 / 03:23